Cheatsheet-v2
The Zen of Python

Python Cheatsheet

  • About
    • Contribute
    • Read It
    • Python Cheatsheet
    • The Zen of Python
    • Python Basics
      • Math Operators
      • Data Types
      • String Concatenation and Replication
      • Variables
      • Comments
      • The print() Function
      • The input() Function
      • The len() Function
      • The str(), int(), and float() Functions
    • Flow Control
      • Comparison Operators
      • Boolean evaluation
      • Boolean Operators
      • Mixing Boolean and Comparison Operators
      • if Statements
      • else Statements
      • elif Statements
      • while Loop Statements
      • break Statements
      • continue Statements
      • for Loops and the range() Function
      • For else statement
      • Importing Modules
      • Ending a Program Early with sys.exit()
    • Functions
      • Return Values and return Statements
      • The None Value
      • Keyword Arguments and print()
      • Local and Global Scope
      • The global Statement
    • Exception Handling
      • Basic exception handling
      • Final code in exception handling
    • Lists
      • Getting Individual Values in a List with Indexes
      • Negative Indexes
      • Getting Sublists with Slices
      • Getting a List’s Length with len()
      • Changing Values in a List with Indexes
      • List Concatenation and List Replication
      • Removing Values from Lists with del Statements
      • Using for Loops with Lists
      • Looping Through Multiple Lists with zip()
      • The in and not in Operators
      • The Multiple Assignment Trick
      • Augmented Assignment Operators
      • Finding a Value in a List with the index() Method
      • Adding Values to Lists with the append() and insert() Methods
      • Removing Values from Lists with remove()
      • Removing Values from Lists with pop()
      • Sorting the Values in a List with the sort() Method
      • Tuple Data Type
      • Converting Types with the list() and tuple() Functions
    • Dictionaries and Structuring Data
      • The keys(), values(), and items() Methods
      • Checking Whether a Key or Value Exists in a Dictionary
      • The get() Method
      • The setdefault() Method
      • Pretty Printing
      • Merge two dictionaries
    • sets
      • Initializing a set
      • sets: unordered collections of unique elements
      • set add() and update()
      • set remove() and discard()
      • set union()
      • set intersection
      • set difference
      • set symetric_difference
    • itertools Module
      • accumulate()
      • combinations()
      • combinations_with_replacement()
      • count()
      • cycle()
      • chain()
      • compress()
      • dropwhile()
      • filterfalse()
      • groupby()
      • islice()
      • permutations()
      • product()
      • repeat()
      • starmap()
      • takewhile()
      • tee()
      • zip_longest()
    • Comprehensions
      • List comprehension
      • Set comprehension
      • Dict comprehension
    • Manipulating Strings
      • Escape Characters
      • Raw Strings
      • Multiline Strings with Triple Quotes
      • Indexing and Slicing Strings
      • The in and not in Operators with Strings
      • The in and not in Operators with list
      • The upper(), lower(), isupper(), and islower() String Methods
      • The isX String Methods
      • The startswith() and endswith() String Methods
      • The join() and split() String Methods
      • Justifying Text with rjust(), ljust(), and center()
      • Removing Whitespace with strip(), rstrip(), and lstrip()
      • Copying and Pasting Strings with the pyperclip Module (need pip install)
    • String Formatting
      • % operator
      • String Formatting (str.format)
      • Lazy string formatting
      • Formatted String Literals or f-strings (Python 3.6+)
      • Template Strings
    • Regular Expressions
      • Matching Regex Objects
      • Grouping with Parentheses
      • Matching Multiple Groups with the Pipe
      • Optional Matching with the Question Mark
      • Matching Zero or More with the Star
      • Matching One or More with the Plus
      • Matching Specific Repetitions with Curly Brackets
      • Greedy and Nongreedy Matching
      • The findall() Method
      • Making Your Own Character Classes
      • The Caret and Dollar Sign Characters
      • The Wildcard Character
      • Matching Everything with Dot-Star
      • Matching Newlines with the Dot Character
      • Review of Regex Symbols
      • Case-Insensitive Matching
      • Substituting Strings with the sub() Method
      • Managing Complex Regexes
    • Handling File and Directory Paths
      • Backslash on Windows and Forward Slash on OS X and Linux
      • The Current Working Directory
      • Creating New Folders
      • Absolute vs. Relative Paths
      • Handling Absolute and Relative Paths
      • Checking Path Validity
      • Finding File Sizes and Folder Contents
      • Copying Files and Folders
      • Moving and Renaming Files and Folders
      • Permanently Deleting Files and Folders
      • Safe Deletes with the send2trash Module
      • Walking a Directory Tree
    • Reading and Writing Files
      • The File Reading/Writing Process
      • Opening and reading files with the open() function
      • Writing to Files
      • Saving Variables with the shelve Module
      • Saving Variables with the pprint.pformat() Function
      • Reading ZIP Files
      • Extracting from ZIP Files
      • Creating and Adding to ZIP Files
    • JSON, YAML and configuration files
      • JSON
      • YAML
      • Anyconfig
    • Debugging
      • Raising Exceptions
      • Getting the Traceback as a String
      • Assertions
      • Logging
      • Logging Levels
      • Disabling Logging
      • Logging to a File
    • Lambda Functions
    • Ternary Conditional Operator
    • args and kwargs
      • Things to Remember(args)
      • Things to Remember(kwargs)
    • Context Manager
      • with statement
      • Writing your own contextmanager using generator syntax
    • __main__ Top-level script environment
      • Advantages
    • setup.py
    • Dataclasses
      • Features
      • Default values
      • Type hints
    • Virtual Environment
      • virtualenv
      • poetry
      • pipenv
      • anaconda

The Zen of Python

Long time Pythoneer Tim Peters succinctly channels the BDFL's guiding principles for Python's design into 20 aphorisms, only 19 of which have been written down.
1
>>> import this
2
The Zen of Python, by Tim Peters
3
4
Beautiful is better than ugly.
5
Explicit is better than implicit.
6
Simple is better than complex.
7
Complex is better than complicated.
8
Flat is better than nested.
9
Sparse is better than dense.
10
Readability counts.
11
Special cases aren't special enough to break the rules.
12
Although practicality beats purity.
13
Errors should never pass silently.
14
Unless explicitly silenced.
15
In the face of ambiguity, refuse the temptation to guess.
16
There should be one-- and preferably only one --obvious way to do it.
17
Although that way may not be obvious at first unless you're Dutch.
18
Now is better than never.
19
Although never is often better than *right* now.
20
If the implementation is hard to explain, it's a bad idea.
21
If the implementation is easy to explain, it may be a good idea.
22
Namespaces are one honking great idea -- let's do more of those!
Copied!
Return to the Top

Python Basics

Math Operators

From Highest to Lowest precedence:
Operators
Operation
Example
**
Exponent
2 ** 3 = 8
%
Modulus/Remainder
22 % 8 = 6
//
Integer division
22 // 8 = 2
/
Division
22 / 8 = 2.75
*
Multiplication
3 * 3 = 9
-
Subtraction
5 - 2 = 3
+
Addition
2 + 2 = 4
Examples of expressions in the interactive shell:
1
>>> 2 + 3 * 6
2
20
Copied!
1
>>> (2 + 3) * 6
2
30
Copied!
1
>>> 2 ** 8
2
256
Copied!
1
>>> 23 // 7
2
3
Copied!
1
>>> 23 % 7
2
2
Copied!
1
>>> (5 - 1) * ((7 + 1) / (3 - 1))
2
16.0
Copied!
Return to the Top

Data Types

Data Type
Examples
Integers
-2, -1, 0, 1, 2, 3, 4, 5
Floating-point numbers
-1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25
Strings
'a', 'aa', 'aaa', 'Hello!', '11 cats'
Return to the Top

String Concatenation and Replication

String concatenation:
1
>>> 'Alice' 'Bob'
2
'AliceBob'
Copied!
Note: Avoid + operator for string concatenation. Prefer string formatting.
String Replication:
1
>>> 'Alice' * 5
2
'AliceAliceAliceAliceAlice'
Copied!
Return to the Top

Variables

You can name a variable anything as long as it obeys the following rules:
  1. 1.
    It can be only one word.
  2. 2.
    It can use only letters, numbers, and the underscore (_) character.
  3. 3.
    It can’t begin with a number.
  4. 4.
    Variable name starting with an underscore (_) are considered as "unuseful`.
Example:
1
>>> spam = 'Hello'
2
>>> spam
3
'Hello'
Copied!
1
>>> _spam = 'Hello'
Copied!
_spam should not be used again in the code.
Return to the Top

Comments

Inline comment:
1
# This is a comment
Copied!
Multiline comment:
1
# This is a
2
# multiline comment
Copied!
Code with a comment:
1
a = 1 # initialization
Copied!
Please note the two spaces in front of the comment.
Function docstring:
1
def foo():
2
"""
3
This is a function docstring
4
You can also use:
5
''' Function Docstring '''
6
"""
Copied!
Return to the Top

The print() Function

1
>>> print('Hello world!')
2
Hello world!
Copied!
1
>>> a = 1
2
>>> print('Hello world!', a)
3
Hello world! 1
Copied!
Return to the Top

The input() Function

Example Code:
1
>>> print('What is your name?') # ask for their name
2
>>> myName = input()
3
>>> print('It is good to meet you, {}'.format(myName))
4
What is your name?
5
Al
6
It is good to meet you, Al
Copied!
Return to the Top

The len() Function

Evaluates to the integer value of the number of characters in a string:
1
>>> len('hello')
2
5
Copied!
Note: test of emptiness of strings, lists, dictionary, etc, should not use len, but prefer direct boolean evaluation.
1
>>> a = [1, 2, 3]
2
>>> if a:
3
>>> print("the list is not empty!")
Copied!
Return to the Top

The str(), int(), and float() Functions

Integer to String or Float:
1
>>> str(29)
2
'29'
Copied!
1
>>> print('I am {} years old.'.format(str(29)))
2
I am 29 years old.
Copied!
1
>>> str(-3.14)
2
'-3.14'
Copied!
Float to Integer:
1
>>> int(7.7)
2
7
Copied!
1
>>> int(7.7) + 1
2
8
Copied!
Return to the Top

Flow Control

Comparison Operators

Operator
Meaning
==
Equal to
!=
Not equal to
<
Less than
>
Greater Than
<=
Less than or Equal to
>=
Greater than or Equal to
These operators evaluate to True or False depending on the values you give them.
Examples:
1
>>> 42 == 42
2
True
Copied!
1
>>> 40 == 42
2
False
Copied!
1
>>> 'hello' == 'hello'
2
True
Copied!
1
>>> 'hello' == 'Hello'
2
False
Copied!
1
>>> 'dog' != 'cat'
2
True
Copied!
1
>>> 42 == 42.0
2
True
Copied!
1
>>> 42 == '42'
2
False
Copied!

Boolean evaluation

Never use == or != operator to evaluate boolean operation. Use the is or is not operators, or use implicit boolean evaluation.
NO (even if they are valid Python):
1
>>> True == True
2
True
Copied!
1
>>> True != False
2
True
Copied!
YES (even if they are valid Python):
1
>>> True is True
2
True
Copied!
1
>>> True is not False
2
True
Copied!
These statements are equivalent:
1
>>> if a is True:
2
>>> pass
3
>>> if a is not False:
4
>>> pass
5
>>> if a:
6
>>> pass
Copied!
And these as well:
1
>>> if a is False:
2
>>> pass
3
>>> if a is not True:
4
>>> pass
5
>>> if not a:
6
>>> pass
Copied!
Return to the Top

Boolean Operators

There are three Boolean operators: and, or, and not.
The and Operator’s Truth Table:
Expression
Evaluates to
True and True
True
True and False
False
False and True
False
False and False
False
The or Operator’s Truth Table:
Expression
Evaluates to
True or True
True
True or False
True
False or True
True
False or False
False
The not Operator’s Truth Table:
Expression
Evaluates to
not True
False
not False
True
Return to the Top

Mixing Boolean and Comparison Operators

1
>>> (4 < 5) and (5 < 6)
2
True
Copied!
1
>>> (4 < 5) and (9 < 6)
2
False
Copied!
1
>>> (1 == 2) or (2 == 2)
2
True
Copied!
You can also use multiple Boolean operators in an expression, along with the comparison operators:
1
>>> 2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2
2
True
Copied!
Return to the Top

if Statements

1
if name == 'Alice':
2
print('Hi, Alice.')
Copied!
Return to the Top

else Statements

1
name = 'Bob'
2
if name == 'Alice':
3
print('Hi, Alice.')
4
else:
5
print('Hello, stranger.')
Copied!
Return to the Top

elif Statements

1
name = 'Bob'
2
age = 5
3
if name == 'Alice':
4
print('Hi, Alice.')
5
elif age < 12:
6
print('You are not Alice, kiddo.')
Copied!
1
name = 'Bob'
2
age = 30
3
if name == 'Alice':
4
print('Hi, Alice.')
5
elif age < 12:
6
print('You are not Alice, kiddo.')
7
else:
8
print('You are neither Alice nor a little kid.')
Copied!
Return to the Top

while Loop Statements

1
spam = 0
2
while spam < 5:
3
print('Hello, world.')
4
spam = spam + 1
Copied!
Return to the Top

break Statements

If the execution reaches a break statement, it immediately exits the while loop’s clause:
1
while True:
2
print('Please type your name.')
3
name = input()
4
if name == 'your name':
5
break
6
print('Thank you!')
Copied!
Return to the Top

continue Statements

When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop.
1
while True:
2
print('Who are you?')
3
name = input()
4
if name != 'Joe':
5
continue
6
print('Hello, Joe. What is the password? (It is a fish.)')
7
password = input()
8
if password == 'swordfish':
9
break
10
print('Access granted.')
Copied!
Return to the Top

for Loops and the range() Function

1
>>> print('My name is')
2
>>> for i in range(5):
3
>>> print('Jimmy Five Times ({})'.format(str(i)))
4
My name is
5
Jimmy Five Times (0)
6
Jimmy Five Times (1)
7
Jimmy Five Times (2)
8
Jimmy Five Times (3)
9
Jimmy Five Times (4)
Copied!
The range() function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration.
1
>>> for i in range(0, 10, 2):
2
>>> print(i)
3
0
4
2
5
4
6
6
7
8
Copied!
You can even use a negative number for the step argument to make the for loop count down instead of up.
1
>>> for i in range(5, -1, -1):
2
>>> print(i)
3
5
4
4
5
3
6
2
7
1
8
0
Copied!

For else statement

This allows to specify a statement to execute in case of the full loop has been executed. Only useful when a break condition can occur in the loop:
1
>>> for i in [1, 2, 3, 4, 5]:
2
>>> if i == 3:
3
>>> break
4
>>> else:
5
>>> print("only executed when no item of the list is equal to 3")
Copied!
Return to the Top

Importing Modules

1
import random
2
for i in range(5):
3
print(random.randint(1, 10))
Copied!
1
import random, sys, os, math
Copied!
1
from random import *
Copied!
Return to the Top

Ending a Program Early with sys.exit()

1
import sys
2
3
while True:
4
print('Type exit to exit.')
5
response = input()
6
if response == 'exit':
7
sys.exit()
8
print('You typed {}.'.format(response))
Copied!
Return to the Top

Functions

1
>>> def hello(name):
2
>>> print('Hello {}'.format(name))
3
>>>
4
>>> hello('Alice')
5
>>> hello('Bob')
6
Hello Alice
7
Hello Bob
Copied!
Return to the Top

Return Values and return Statements

When creating a function using the def statement, you can specify what the return value should be with a return statement. A return statement consists of the following:
  • The return keyword.
  • The value or expression that the function should return.
1
import random
2
def getAnswer(answerNumber):
3
if answerNumber == 1:
4
return 'It is certain'
5
elif answerNumber == 2:
6
return 'It is decidedly so'
7
elif answerNumber == 3:
8
return 'Yes'
9
elif answerNumber == 4:
10
return 'Reply hazy try again'
11
elif answerNumber == 5:
12
return 'Ask again later'
13
elif answerNumber == 6:
14
return 'Concentrate and ask again'
15
elif answerNumber == 7:
16
return 'My reply is no'
17
elif answerNumber == 8:
18
return 'Outlook not so good'
19
elif answerNumber == 9:
20
return 'Very doubtful'
21
22
r = random.randint(1, 9)
23
fortune = getAnswer(r)
24
print(fortune)
Copied!
Return to the Top

The None Value

1
>>> spam = print('Hello!')
2
Hello!
Copied!
1
>>> spam is None
2
True
Copied!
Note: never compare to None with the == operator. Always use is.
Return to the Top

Keyword Arguments and print()

1
>>> print('Hello', end='')
2
>>> print('World')
3
HelloWorld
Copied!
1
>>> print('cats', 'dogs', 'mice')
2
cats dogs mice
Copied!
1
>>> print('cats', 'dogs', 'mice', sep=',')
2
cats,dogs,mice
Copied!
Return to the Top

Local and Global Scope

  • Code in the global scope cannot use any local variables.
  • However, a local scope can access global variables.
  • Code in a function’s local scope cannot use variables in any other local scope.
  • You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam.
Return to the Top

The global Statement

If you need to modify a global variable from within a function, use the global statement:
1
>>> def spam():
2
>>> global eggs
3
>>> eggs = 'spam'
4
>>>
5
>>> eggs = 'global'
6
>>> spam()
7
>>> print(eggs)
8
spam
Copied!
There are four rules to tell whether a variable is in a local scope or global scope:
  1. 1.
    If a variable is being used in the global scope (that is, outside of all functions), then it is always a global variable.
  2. 2.
    If there is a global statement for that variable in a function, it is a global variable.
  3. 3.
    Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.
  4. 4.
    But if the variable is not used in an assignment statement, it is a global variable.
Return to the Top

Exception Handling

Basic exception handling

1
>>> def spam(divideBy):
2
>>> try:
3
>>> return 42 / divideBy
4
>>> except ZeroDivisionError as e:
5
>>> print('Error: Invalid argument: {}'.format(e))
6
>>>
7
>>> print(spam(2))
8
>>> print(spam(12))
9
>>> print(spam(0))
10
>>> print(spam(1))
11
21.0
12
3.5
13
Error: Invalid argument: division by zero
14
None
15
42.0
Copied!
Return to the Top

Final code in exception handling

Code inside the finally section is always executed, no matter if an exception has been raised or not, and even if an exception is not caught.
1
>>> def spam(divideBy):
2
>>> try:
3
>>> return 42 / divideBy
4
>>> except ZeroDivisionError as e:
5
>>> print('Error: Invalid argument: {}'.format(e))
6
>>> finally:
7
>>> print("-- division finished --")
8
>>> print(spam(2))
9
-- division finished --
10
21.0
11
>>> print(spam(12))
12
-- division finished --
13
3.5
14
>>> print(spam(0))
15
Error: Invalid Argument division by zero
16
-- division finished --
17
None
18
>>> print(spam(1))
19
-- division finished --
20
42.0
Copied!
Return to the Top

Lists

1
>>> spam = ['cat', 'bat', 'rat', 'elephant']
2
3
>>> spam
4
['cat', 'bat', 'rat', 'elephant']
Copied!
Return to the Top

Getting Individual Values in a List with Indexes

1
>>> spam = ['cat', 'bat', 'rat', 'elephant']
2
>>> spam[0]
3
'cat'
Copied!
1
>>> spam[1]
2
'bat'
Copied!
1
>>> spam[2]
2
'rat'
Copied!
1
>>> spam[3]
2
'elephant'
Copied!
Return to the Top

Negative Indexes

1
>>> spam = ['cat', 'bat', 'rat', 'elephant']
2
>>> spam[-1]
3
'elephant'
Copied!
1
>>> spam[-3]
2
'bat'
Copied!
1
>>> 'The {} is afraid of the {}.'.format(spam[-1], spam[-3])
2
'The elephant is afraid of the bat.'
Copied!
Return to the Top

Getting Sublists with Slices

1
>>> spam = ['cat', 'bat', 'rat', 'elephant']
2
>>> spam[0:4]
3
['cat', 'bat', 'rat', 'elephant']
Copied!
1
>>> spam[1:3]
2
['bat', 'rat']
Copied!
1
>>> spam[0:-1]
2
['cat', 'bat', 'rat']
Copied!
1
>>> spam = ['cat', 'bat', 'rat', 'elephant']
2
>>> spam[:2]
3
['cat', 'bat']
Copied!
1
>>> spam[1:]
2
['bat', 'rat', 'elephant']
Copied!
Slicing the complete list will perform a copy:
1
>>> spam2 = spam[:]
2
['cat', 'bat', 'rat', 'elephant']
3
>>> spam.append('dog')
4
>>> spam
5
['cat', 'bat', 'rat', 'elephant', 'dog']
6
>>> spam2
7
['cat', 'bat', 'rat', 'elephant']
Copied!
Return to the Top

Getting a List’s Length with len()

1
>>> spam = ['cat', 'dog', 'moose']
2
>>> len(spam)
3
3
Copied!
Return to the Top

Changing Values in a List with Indexes

1
>>> spam = ['cat', 'bat', 'rat', 'elephant']
2
>>> spam[1] = 'aardvark'
3
4
>>> spam
5
['cat', 'aardvark', 'rat', 'elephant']
6
7
>>> spam[2] = spam[1]
8
9
>>> spam
10
['cat', 'aardvark', 'aardvark', 'elephant']
11
12
>>> spam[-1] = 12345
13
14
>>> spam
15
['cat', 'aardvark', 'aardvark', 12345]
Copied!
Return to the Top

List Concatenation and List Replication

1
>>> [1, 2, 3] + ['A', 'B', 'C']
2
[1, 2, 3, 'A', 'B', 'C']
3
4
>>> ['X', 'Y', 'Z'] * 3
5
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
6
7
>>> spam = [1, 2, 3]
8
9
>>> spam = spam + ['A', 'B', 'C']
10
11
>>> spam
12
[1, 2, 3, 'A', 'B', 'C']
Copied!
Return to the Top

Removing Values from Lists with del Statements

1
>>> spam = ['cat', 'bat', 'rat', 'elephant']
2
>>> del spam[2]
3
>>> spam
4
['cat', 'bat', 'elephant']
Copied!
1
>>> del spam[2]
2
>>> spam
3
['cat', 'bat']
Copied!
Return to the Top

Using for Loops with Lists

1
>>> supplies = ['pens', 'staplers', 'flame-throwers', 'binders']
2
>>> for i, supply in enumerate(supplies):
3
>>> print('Index {} in supplies is: {}'.format(str(i), supply))
4
Index 0 in supplies is: pens
5
Index 1 in supplies is: staplers
6
Index 2 in supplies is: flame-throwers
7
Index 3 in supplies is: binders
Copied!
Return to the Top

Looping Through Multiple Lists with zip()

1
>>> name = ['Pete', 'John', 'Elizabeth']
2
>>> age = [6, 23, 44]
3
>>> for n, a in zip(name, age):
4
>>> print('{} is {} years old'.format(n, a))
5
Pete is 6 years old
6
John is 23 years old
7
Elizabeth is 44 years old
Copied!

The in and not in Operators

1
>>> 'howdy' in ['hello', 'hi', 'howdy', 'heyas']
2
True
Copied!
1
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
2
>>> 'cat' in spam
3
False
Copied!
1
>>> 'howdy' not in spam
2
False
Copied!
1
>>> 'cat' not in spam
2
True
Copied!
Return to the Top

The Multiple Assignment Trick

The multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:
1
>>> cat = ['fat', 'orange', 'loud']
2
3
>>> size = cat[0]
4
5
>>> color = cat[1]
6
7
>>> disposition = cat[2]
Copied!
You could type this line of code:
1
>>> cat = ['fat', 'orange', 'loud']
2
3
>>> size, color, disposition = cat
Copied!
The multiple assignment trick can also be used to swap the values in two variables:
1
>>> a, b = 'Alice', 'Bob'
2
>>> a, b = b, a
3
>>> print(a)
4
'Bob'
Copied!
1
>>> print(b)
2
'Alice'
Copied!
Return to the Top

Augmented Assignment Operators

Operator
Equivalent
spam += 1
spam = spam + 1
spam -= 1
spam = spam - 1
spam *= 1
spam = spam * 1
spam /= 1
spam = spam / 1
spam %= 1
spam = spam % 1
Examples:
1
>>> spam = 'Hello'
2
>>> spam += ' world!'
3
>>> spam
4
'Hello world!'
5
6
>>> bacon = ['Zophie']
7
>>> bacon *= 3
8
>>> bacon
9
['Zophie', 'Zophie', 'Zophie']
Copied!
Return to the Top

Finding a Value in a List with the index() Method

1
>>> spam = ['Zophie', 'Pooka', 'Fat-tail', 'Pooka']
2
3
>>> spam.index('Pooka')
4
1
Copied!
Return to the Top

Adding Values to Lists with the append() and insert() Methods

append():
1
>>> spam = ['cat', 'dog', 'bat']
2
3
>>> spam.append('moose')
4
5
>>> spam
6
['cat', 'dog', 'bat', 'moose']
Copied!
insert():
1
>>> spam = ['cat', 'dog', 'bat']
2
3
>>> spam.insert(1, 'chicken')
4
5
>>> spam
6
['cat', 'chicken', 'dog', 'bat']
Copied!
Return to the Top

Removing Values from Lists with remove()

1
>>> spam = ['cat', 'bat', 'rat', 'elephant']
2
3
>>> spam.remove('bat')
4
5
>>> spam
6
['cat', 'rat', 'elephant']
Copied!
If the value appears multiple times in the list, only the first instance of the value will be removed.
Return to the Top

Removing Values from Lists with pop()

1
>>> spam = ['cat', 'bat', 'rat', 'elephant']
2
3
>>> spam.pop()
4
'elephant'
5
6
>>> spam
7
['cat', 'bat', 'rat']
8
9
>>> spam.pop(0)
10
'cat'
11
12
>>> spam
13
['bat', 'rat']
Copied!
Return to the Top

Sorting the Values in a List with the sort() Method

1
>>> spam = [2, 5, 3.14, 1, -7]
2
>>> spam.sort()
3
>>> spam
4
[-7, 1, 2, 3.14, 5]
Copied!
1
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
2
>>> spam.sort()
3
>>> spam
4
['ants', 'badgers', 'cats', 'dogs', 'elephants']
Copied!
You can also pass True for the reverse keyword argument to have sort() sort the values in reverse order:
1
>>> spam.sort(reverse=True)
2
>>> spam
3
['elephants', 'dogs', 'cats', 'badgers', 'ants']
Copied!
If you need to sort the values in regular alphabetical order, pass str. lower for the key keyword argument in the sort() method call:
1
>>> spam = ['a', 'z', 'A', 'Z']
2
>>> spam.sort(key=str.lower)
3
>>> spam
4
['a', 'A', 'z', 'Z']
Copied!
You can use the built-in function sorted to return a new list:
1
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
2
>>> sorted(spam)
3
['ants', 'badgers', 'cats', 'dogs', 'elephants']
Copied!
Return to the Top

Tuple Data Type

1
>>> eggs = ('hello', 42, 0.5)
2
>>> eggs[0]
3
'hello'
Copied!
1
>>> eggs[1:3]
2
(42, 0.5)
Copied!
1
>>> len(eggs)
2
3
Copied!
The main way that tuples are different from lists is that tuples, like strings, are immutable.
Return to the Top

Converting Types with the list() and tuple() Functions

1
>>> tuple(['cat', 'dog', 5])
2
('cat', 'dog', 5)
Copied!
1
>>> list(('cat', 'dog', 5))
2
['cat', 'dog', 5]
Copied!
1
>>> list('hello')
2
['h', 'e', 'l', 'l', 'o']
Copied!
Return to the Top

Dictionaries and Structuring Data

Example Dictionary:
1
myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}
Copied!
Return to the Top

The keys(), values(), and items() Methods

values():
1
>>> spam = {'color': 'red', 'age': 42}
2
>>> for v in spam.values():
3
>>> print(v)
4
red
5
42
Copied!
keys():
1
>>> for k in spam.keys():
2
>>> print(k)
3
color
4
age
Copied!
items():
1
>>> for i in spam.items():
2
>>> print(i)
3
('color', 'red')
4
('age', 42)
Copied!
Using the keys(), values(), and items() methods, a for loop can iterate over the keys, values, or key-value pairs in a dictionary, respectively.
1
>>> spam = {'color': 'red', 'age': 42}
2
>>>
3
>>> for k, v in spam.items():
4
>>> print('Key: {} Value: {}'.format(k, str(v)))
5
Key: age Value: 42
6
Key: color Value: red
Copied!
Return to the Top

Checking Whether a Key or Value Exists in a Dictionary

1
>>> spam = {'name': 'Zophie', 'age': 7}
Copied!
1
>>> 'name' in spam.keys()
2
True
Copied!
1
>>> 'Zophie' in spam.values()
2
True
Copied!
1
>>> # You can omit the call to keys() when checking for a key
2
>>> 'color' in spam
3
False
Copied!
1
>>> 'color' not in spam
2
True
Copied!
Return to the Top

The get() Method

Get has two parameters: key and default value if the key did not exist
1
>>> picnic_items = {'apples': 5, 'cups': 2}
2
3
>>> 'I am bringing {} cups.'.format(str(picnic_items.get('cups', 0)))
4
'I am bringing 2 cups.'
Copied!
1
>>> 'I am bringing {} eggs.'.format(str(picnic_items.get('eggs', 0)))
2
'I am bringing 0 eggs.'
Copied!
Return to the Top

The setdefault() Method

Let's consider this code:
1
spam = {'name': 'Pooka', 'age': 5}
2
3
if 'color' not in spam:
4
spam['color'] = 'black'
Copied!
Using setdefault we could write the same code more succinctly:
1
>>> spam = {'name': 'Pooka', 'age': 5}
2
>>> spam.setdefault('color', 'black')
3
'black'
Copied!
1
>>> spam
2
{'color': 'black', 'age': 5, 'name': 'Pooka'}
Copied!
1
>>> spam.setdefault('color', 'white')
2
'black'
Copied!
1
>>> spam
2
{'color': 'black', 'age': 5, 'name': 'Pooka'}
Copied!
Return to the Top

Pretty Printing

1
>>> import pprint
2
>>>
3
>>> message = 'It was a bright cold day in April, and the clocks were striking
4
>>> thirteen.'
5
>>> count = {}
6
>>>
7
>>> for character in message:
8
>>> count.setdefault(character, 0)
9
>>> count[character] = count[character] + 1
10
>>>
11
>>> pprint.pprint(count)
12
{' ': 13,
13
',': 1,
14
'.': 1,
15
'A': 1,
16
'I': 1,
17
'a': 4,
18
'b': 1,
19
'c': 3,
20
'd': 3,
21
'e': 5,
22
'g': 2,
23
'h': 3,
24
'i': 6,
25
'k': 2,
26
'l': 3,
27
'n': 4,
28
'o': 2,
29
'p': 1,
30
'r': 5,
31
's': 3,
32
't': 6,
33
'w': 2,
34
'y': 1}
Copied!
Return to the Top

Merge two dictionaries

1
# in Python 3.5+:
2
>>> x = {'a': 1, 'b': 2}
3
>>> y = {'b': 3, 'c': 4}
4
>>> z = {**x, **y}
5
>>> z
6
{'c': 4, 'a': 1, 'b': 3}
7
8
# in Python 2.7
9
>>> z = dict(x, **y)
10
>>> z
11
{'c': 4, 'a': 1, 'b': 3}
Copied!

sets

From the Python 3 documentation
A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.

Initializing a set

There are two ways to create sets: using curly braces {} and the built-in function set()
1
>>> s = {1, 2, 3}
2
>>> s = set([1, 2, 3])
Copied!
When creating an empty set, be sure to not use the curly braces {} or you will get an empty dictionary instead.
1
>>> s = {}
2
>>> type(s)
3
<class 'dict'>
Copied!

sets: unordered collections of unique elements

A set automatically remove all the duplicate values.
1
>>> s = {1, 2, 3, 2, 3, 4}
2
>>> s
3
{1, 2, 3, 4}
Copied!
And as an unordered data type, they can't be indexed.
1
>>> s = {1, 2, 3}
2
>>> s[0]
3
Traceback (most recent call last):
4
File "<stdin>", line 1, in <module>
5
TypeError: 'set' object does not support indexing
6
>>>
Copied!

set add() and update()

Using the add() method we can add a single element to the set.
1
>>> s = {1, 2, 3}
2
>>> s.add(4)
3
>>> s
4
{1, 2, 3, 4}
Copied!
And with update(), multiple ones .
1
>>> s = {1, 2, 3}
2
>>> s.update([2, 3, 4, 5, 6])
3
>>> s
4
{1, 2, 3, 4, 5, 6} # remember, sets automatically remove duplicates
Copied!

set remove() and discard()

Both methods will remove an element from the set, but remove() will raise a key error if the value doesn't exist.
1
>>> s = {1, 2, 3}
2
>>> s.remove(3)
3
>>> s
4
{1, 2}
5
>>> s.remove(3)
6
Traceback (most recent call last):
7
File "<stdin>", line 1, in <module>
8
KeyError: 3
Copied!
discard() won't raise any errors.
1
>>> s = {1, 2, 3}
2
>>> s.discard(3)
3
>>> s
4
{1, 2}
5
>>> s.discard(3)
6
>>>
Copied!

set union()

union() or | will create a new set that contains all the elements from the sets provided.
1
>>> s1 = {1, 2, 3}
2
>>> s2 = {3, 4, 5}
3
>>> s1.union(s2) # or 's1 | s2'
4
{1, 2, 3, 4, 5}
Copied!

set intersection

intersection or & will return a set containing only the elements that are common to all of them.
1
>>> s1 = {1, 2, 3}
2
>>> s2 = {2, 3, 4}
3
>>> s3 = {3, 4, 5}
4
>>> s1.intersection(s2, s3) # or 's1 & s2 & s3'
5
{3}
Copied!

set difference

difference or - will return only the elements that are unique to the first set (invoked set).
1
>>> s1 = {1, 2, 3}
2
>>> s2 = {2, 3, 4}
3
>>> s1.difference(s2) # or 's1 - s2'
4
{1}
5
>>> s2.difference(s1) # or 's2 - s1'
6
{4}
Copied!

set symetric_difference

symetric_difference or ^ will return all the elements that are not common between them.
1
>>> s1 = {1, 2, 3}
2
>>> s2 = {2, 3, 4}
3
>>> s1.symmetric_difference(s2) # or 's1 ^ s2'
4
{1, 4}
Copied!
Return to the Top

itertools Module

The itertools module is a collection of tools intended to be fast and use memory efficiently when handling iterators (like lists or dictionaries).
From the official Python 3.x documentation:
The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.
The itertools module comes in the standard library and must be imported.
The operator module will also be used. This module is not necessary when using itertools, but needed for some of the examples below.
Return to the Top

accumulate()

Makes an iterator that returns the results of a function.
1
itertools.accumulate(iterable[, func])
Copied!
Example:
1
>>> data = [1, 2, 3, 4, 5]
2
>>> result = itertools.accumulate(data, operator.mul)
3
>>> for each in result:
4
>>> print(each)
5
1
6
2
7
6
8
24
9
120
Copied!
The operator.mul takes two numbers and multiplies them:
1
operator.mul(1, 2)
2
2
3
operator.mul(2, 3)
4
6
5
operator.mul(6, 4)
6
24
7
operator.mul(24, 5)
8
120
Copied!
Passing a function is optional:
1
>>> data = [5, 2, 6, 4, 5, 9, 1]
2
>>> result = itertools.accumulate(data)
3
>>> for each in result:
4
>>> print(each)
5
5
6
7
7
13
8
17
9
22
10
31
11
32
Copied!
If no function is designated the items will be summed:
1
5
2
5 + 2 = 7
3
7 + 6 = 13
4
13 + 4 = 17
5
17 + 5 = 22
6
22 + 9 = 31
7
31 + 1 = 32
Copied!
Return to the Top

combinations()

Takes an iterable and a integer. This will create all the unique combination that have r members.
1
itertools.combinations(iterable, r)
Copied!
Example:
1
>>> shapes = ['circle', 'triangle', 'square',]
2
>>> result = itertools.combinations(shapes, 2)
3
>>> for each in result:
4
>>> print(each)
5
('circle', 'triangle')
6
('circle', 'square')
7
('triangle', 'square')
Copied!
Return to the Top

combinations_with_replacement()

Just like combinations(), but allows individual elements to be repeated more than once.
1
itertools.combinations_with_replacement(iterable, r)
Copied!
Example:
1
>>> shapes = ['circle', 'triangle', 'square']
2
>>> result = itertools.combinations_with_replacement(shapes, 2)
3
>>> for each in result:
4
>>> print(each)
5
('circle', 'circle')
6
('circle', 'triangle')
7
('circle', 'square')
8
('triangle', 'triangle')
9
('triangle', 'square')
10
('square', 'square')
Copied!
Return to the Top

count()

Makes an iterator that returns evenly spaced values starting with number start.
1
itertools.count(start=0, step=1)
Copied!
Example:
1
>>> for i in itertools.count(10,3):
2
>>> print(i)
3
>>> if i > 20:
4
>>> break
5
10
6
13
7
16
8
19
9
22
Copied!
Return to the Top

cycle()

This function cycles through an iterator endlessly.
1
itertools.cycle(iterable)
Copied!
Example:
1
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet']
2
>>> for color in itertools.cycle(colors):
3
>>> print(color)
4
red
5
orange
6
yellow
7
green
8
blue
9
violet
10
red
11
orange
Copied!
When reached the end of the iterable it start over again from the beginning.
Return to the Top

chain()

Take a series of iterables and return them as one long iterable.
1
itertools.chain(*iterables)
Copied!
Example:
1
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
2
>>> shapes = ['circle', 'triangle', 'square', 'pentagon']
3
>>> result = itertools.chain(colors, shapes)
4
>>> for each in result:
5
>>> print(each)
6
red
7
orange
8
yellow
9
green
10
blue
11
circle
12
triangle
13
square
14
pentagon
Copied!
Return to the Top

compress()

Filters one iterable with another.
1
itertools.compress(data, selectors)
Copied!
Example:
1
>>> shapes = ['circle', 'triangle', 'square', 'pentagon']
2
>>> selections = [True, False, True, False]
3
>>> result = itertools.compress(shapes, selections)
4
>>> for each in result:
5
>>> print(each)
6
circle
7
square
Copied!
Return to the Top

dropwhile()

Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.
1
itertools.dropwhile(predicate, iterable)
Copied!
Example:
1
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
2
>>> result = itertools.dropwhile(lambda x: x<5, data)
3
>>> for each in result:
4
>>> print(each)
5
5
6
6
7
7
8
8
9
9
10
10
11
1
Copied!
Return to the Top

filterfalse()

Makes an iterator that filters elements from iterable returning only those for which the predicate is False.
1
itertools.filterfalse(predicate, iterable)
Copied!
Example:
1
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
2
>>> result = itertools.filterfalse(lambda x: x<5, data)
3
>>> for each in result:
4
>>> print(each)
5
5
6
6
7
7
8
8
9
9
10
10
Copied!
Return to the Top

groupby()

Simply put, this function groups things together.
1
itertools.groupby(iterable, key=None)
Copied!
Example:
1
>>> robots = [{
2
'name': 'blaster',
3
'faction': 'autobot'
4
}, {
5
'name': 'galvatron',
6
'faction': 'decepticon'
7
}, {
8
'name': 'jazz',
9
'faction': 'autobot'
10
}, {
11
'name': 'metroplex',
12
'faction': 'autobot'
13
}, {
14
'name': 'megatron',
15
'faction': 'decepticon'
16
}, {
17
'name': 'starcream',
18
'faction': 'decepticon'
19
}]
20
>>> for key, group in itertools.groupby(robots, key=lambda x: x['faction']):
21
>>> print(key)
22
>>> print(list(group))
23
autobot
24
[{'name': 'blaster', 'faction': 'autobot'}]
25
decepticon
26
[{'name': 'galvatron', 'faction': 'decepticon'}]
27
autobot
28
[{'name': 'jazz', 'faction': 'autobot'}, {'name': 'metroplex', 'faction': 'autobot'}]
29
decepticon
30
[{'name': 'megatron', 'faction': 'decepticon'}, {'name': 'starcream', 'faction': 'decepticon'}]
Copied!
Return to the Top

islice()

This function is very much like slices. This allows you to cut out a piece of an iterable.
1
itertools.islice(iterable, start, stop[, step])
Copied!
Example:
1
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue',]
2
>>> few_colors = itertools.islice(colors, 2)
3
>>> for each in few_colors:
4
>>> print(each)
5
red
6
orange
Copied!
Return to the Top

permutations()

1
itertools.permutations(iterable, r=None)
Copied!
Example:
1
>>> alpha_data = ['a', 'b', 'c']
2
>>> result = itertools.permutations(alpha_data)
3
>>> for each in result:
4
>>> print(each)
5
('a', 'b', 'c')
6
('a', 'c', 'b')
7
('b', 'a', 'c')
8
('b', 'c', 'a')
9
('c', 'a', 'b')
10
('c', 'b', 'a')
Copied!
Return to the Top

product()

Creates the cartesian products from a series of iterables.
1
>>> num_data = [1, 2, 3]
2
>>> alpha_data = ['a', 'b', 'c']
3
>>> result = itertools.product(num_data, alpha_data)
4
>>> for each in result:
5
print(each)
6
(1, 'a')
7
(1, 'b')
8
(1, 'c')
9
(2, 'a')
10
(2, 'b')
11
(2, 'c')
12
(3, 'a')
13
(3, 'b')
14
(3, 'c')
Copied!
Return to the Top

repeat()

This function will repeat an object over and over again. Unless, there is a times argument.
1
itertools.repeat(object[, times])
Copied!
Example:
1
>>> for i in itertools.repeat("spam", 3):
2
print(i)
3
spam
4
spam
5
spam
Copied!
Return to the Top

starmap()

Makes an iterator that computes the function using arguments obtained from the iterable.
1
itertools.starmap(function, iterable)
Copied!
Example:
1
>>> data = [(2, 6), (8, 4), (7, 3)]
2
>>> result = itertools.starmap(operator.mul, data)
3
>>> for each in result:
4
>>> print(each)
5
12
6
32
7
21
Copied!
Return to the Top

takewhile()

The opposite of dropwhile(). Makes an iterator and returns elements from the iterable as long as the predicate is true.
1
itertools.takewhile(predicate, iterable)
Copied!
Example:
1
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
2
>>> result = itertools.takewhile(lambda x: x<5, data)
3
>>> for each in result:
4
>>> print(each)
5
1
6
2
7
3
8
4
Copied!
Return to the Top

tee()

Return n independent iterators from a single iterable.
1
itertools.tee(iterable, n=2)
Copied!
Example:
1
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
2
>>> alpha_colors, beta_colors = itertools.tee(colors)
3
>>> for each in alpha_colors:
4
>>> print(each)
5
red
6
orange
7
yellow
8
green
9
blue
Copied!
1
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
2
>>> alpha_colors, beta_colors = itertools.tee(colors)
3
>>> for each in beta_colors:
4
>>> print(each)
5
red
6
orange
7
yellow
8
green
9
blue
Copied!
Return to the Top

zip_longest()

Makes an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.
1
itertools.zip_longest(*iterables, fillvalue=None)
Copied!
Example:
1
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue',]
2
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,]
3
>>> for each in itertools.zip_longest(colors, data, fillvalue=None):
4
>>> print(each)
5
('red', 1)
6
('orange', 2)
7
('yellow', 3)
8
('green', 4)
9
('blue', 5)
10
(None, 6)
11
(None, 7)
12
(None, 8)
13
(None, 9)
14
(None, 10)
Copied!
Return to the Top

Comprehensions

List comprehension

1
>>> a = [1, 3, 5, 7, 9, 11]
2
3
>>> [i - 1 for i in a]
4
[0, 2, 4, 6, 8, 10]
Copied!

Set comprehension

1
>>> b = {"abc", "def"}
2
>>> {s.upper() for s in b}
3
{"ABC", "DEF"}
Copied!

Dict comprehension

1
>>> c = {'name': 'Pooka', 'age': 5}
2
>>> {v: k for k, v in c.items()}
3
{'Pooka': 'name', 5: 'age'}
Copied!
A List comprehension can be generated from a dictionary:
1
>>> c = {'name': 'Pooka', 'first_name': 'Oooka'}
2
>>> ["{}:{}".format(k.upper(), v.upper()) for k, v in c.items()]
3
['NAME:POOKA', 'FIRST_NAME:OOOKA']
Copied!

Manipulating Strings

Escape Characters

Escape character
Prints as
\'
Single quote
\"
Double quote
\t
Tab
\n
Newline (line break)
\\
Backslash
Example:
1
>>> print("Hello there!\nHow are you?\nI\'m doing fine.")
2
Hello there!
3
How are you?
4
I'm doing fine.
Copied!
Return to the Top

Raw Strings

A raw string completely ignores all escape characters and prints any backslash that appears in the string.
1
>>> print(r'That is Carol\'s cat.')
2
That is Carol\'s cat.
Copied!
Note: mostly used for regular expression definition (see re package)
Return to the Top

Multiline Strings with Triple Quotes

1
>>> print('''Dear Alice,
2
>>>
3
>>> Eve's cat has been arrested for catnapping, cat burglary, and extortion.
4
>>>
5
>>> Sincerely,
6
>>> Bob''')
7
Dear Alice,
8
9
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
10
11
Sincerely,
12
Bob
Copied!
To keep a nicer flow in your code, you can use the dedent function from the textwrap standard package.
1
>>> from textwrap import dedent
2
>>>
3
>>> def my_function():
4
>>> print('''
5
>>> Dear Alice,
6
>>>
7
>>> Eve's cat has been arrested for catnapping, cat burglary, and extortion.
8
>>>
9
>>> Sincerely,
10
>>> Bob
11
>>> ''').strip()
Copied!
This generates the same string than before.
Return to the Top

Indexing and Slicing Strings

1
H e l l o w o r l d !
2
0 1 2 3 4 5 6 7 8 9 10 11
Copied!
1
>>> spam = 'Hello world!'
2
3
>>> spam[0]
4
'H'
Copied!
1
>>> spam[4]
2
'o'
Copied!
1
>>> spam[-1]
2
'!'
Copied!
Slicing:
1
>>> spam[0:5]
2
'Hello'
Copied!
1
>>> spam[:5]
2
'Hello'
Copied!
1
>>> spam[6:]
2
'world!'
Copied!
1
>>> spam[6:-1]
2
'world'
Copied!
1
>>> spam[:-1]
2
'Hello world'
Copied!
1
>>> spam[::-1]
2
'!dlrow olleH'
Copied!
1
>>> spam = 'Hello world!'
2
>>> fizz = spam[0:5]
3
>>> fizz
4
'Hello'
Copied!
Return to the Top

The in and not in Operators with Strings

1
>>> 'Hello' in 'Hello World'
2
True
Copied!
1
>>> 'Hello' in 'Hello'
2
True
Copied!
1
>>> 'HELLO' in 'Hello World'
2
False
Copied!
1
>>> '' in 'spam'
2
True
Copied!
1
>>> 'cats' not in 'cats and dogs'
2
False
Copied!

The in and not in Operators with list

1
>>> a = [1, 2, 3, 4]
2
>>> 5 in a
3
False
Copied!
1
>>> 2 in a
2
True
Copied!
Return to the Top

The upper(), lower(), isupper(), and islower() String Methods

upper() and lower():
1
>>> spam = 'Hello world!'
2
>>> spam = spam.upper()
3
>>> spam
4
'HELLO WORLD!'
Copied!
1
>>> spam = spam.lower()
2
>>> spam
3
'hello world!'
Copied!
isupper() and islower():
1
>>> spam = 'Hello world!'
2
>>> spam.islower()
3
False
Copied!
1
>>> spam.isupper()
2
False
Copied!
1
>>> 'HELLO'.isupper()
2
True
Copied!
1
>>> 'abc12345'.islower()
2
True
Copied!
1
>>> '12345'.islower()
2
False
Copied!
1
>>> '12345'.isupper()
2
False
Copied!
Return to the Top

The isX String Methods

  • isalpha() returns True if the string consists only of letters and is not blank.
  • isalnum() returns True if the string consists only of letters and numbers and is not blank.
  • isdecimal() returns True if the string consists only of numeric characters and is not blank.
  • isspace() returns True if the string consists only of spaces,tabs, and new-lines and is not blank.
  • istitle() returns True if the string consists only of words that begin with an uppercase letter followed by only lowercase letters.
Return to the Top

The startswith() and endswith() String Methods

1
>>> 'Hello world!'.startswith('Hello')
2
True
Copied!
1
>>> 'Hello world!'.endswith('world!')
2
True
Copied!
1
>>> 'abc123'.startswith('abcdef')
2
False
Copied!
1
>>> 'abc123'.endswith('12')
2
False
Copied!
1
>>> 'Hello world!'.startswith('Hello world!')
2
True
Copied!
1
>>> 'Hello world!'.endswith('Hello world!')
2
True
Copied!
Return to the Top

The join() and split() String Methods

join():
1
>>> ', '.join(['cats', 'rats', 'bats'])
2
'cats, rats, bats'
Copied!
1
>>> ' '.join(['My', 'name', 'is', 'Simon'])
2
'My name is Simon'
Copied!
1
>>> 'ABC'.join(['My', 'name', 'is', 'Simon'])
2
'MyABCnameABCisABCSimon'
Copied!
split():
1
>>> 'My name is Simon'.split()
2
['My', 'name', 'is', 'Simon']
Copied!
1
>>> 'MyABCnameABCisABCSimon'.split('ABC')
2
['My', 'name', 'is', 'Simon']
Copied!
1
>>> 'My name is Simon'.split('m')
2
['My na', 'e is Si', 'on']
Copied!
Return to the Top

Justifying Text with rjust(), ljust(), and center()

rjust() and ljust():
1
>>> 'Hello'.rjust(10)
2
' Hello'
Copied!
1
>>> 'Hello'.rjust(20)
2
' Hello'
Copied!
1
>>> 'Hello World'.rjust(20)
2
' Hello World'
Copied!
1
>>> 'Hello'.ljust(10)
2
'Hello '
Copied!
An optional second argument to rjust() and ljust() will specify a fill character other than a space character. Enter the following into the interactive shell:
1
>>> 'Hello'.rjust(20, '*')
2
'***************Hello'
Copied!
1
>>> 'Hello'.ljust(20, '-')
2
'Hello---------------'
Copied!
center():
1
>>> 'Hello'.center(20)
2
' Hello '
Copied!
1
>>> 'Hello'.center(20, '=')
2
'=======Hello========'
Copied!
Return to the Top

Removing Whitespace with strip(), rstrip(), and lstrip()

1
>>> spam = ' Hello World '
2
>>> spam.strip()
3
'Hello World'
Copied!
1
>>> spam.lstrip()
2
'Hello World '
Copied!
1
>>> spam.rstrip()
2
' Hello World'
Copied!
1
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
2
>>> spam.strip('ampS')
3
'BaconSpamEggs'
Copied!
Return to the Top

Copying and Pasting Strings with the pyperclip Module (need pip install)

1
>>> import pyperclip
2
3
>>> pyperclip.copy('Hello world!')
4
5
>>> pyperclip.paste()
6
'Hello world!'
Copied!
Return to the Top

String Formatting

% operator

1
>>> name = 'Pete'
2
>>> 'Hello %s' % name
3
"Hello Pete"
Copied!
We can use the %x format specifier to convert an int value to a string:
1
>>> num = 5
2
>>> 'I have %x apples' % num
3
"I have 5 apples"
Copied!
Note: For new code, using str.format or f-strings (Python 3.6+) is strongly recommended over the % operator.
Return to the Top

String Formatting (str.format)

Python 3 introduced a new way to do string formatting that was later back-ported to Python 2.7. This makes the syntax for string formatting more regular.
1
>>> name = 'John'
2
>>> age = 20'
3
4
>>> "Hello I'm {}, my age is {}".format(name, age)
5
"Hello I'm John, my age is 20"
Copied!
1
>>> "Hello I'm {0}, my age is {1}".format(name, age)
2
"Hello I'm John, my age is 20"
Copied!
The official Python 3.x documentation recommend str.format over the % operator:
The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals or the str.format() interface helps avoid these errors. These alternatives also provide more powerful, flexible and extensible approaches to formatting text.
Return to the Top

Lazy string formatting

You would only use %s string formatting on functions that can do lazy parameters evaluation, the most common being logging:
Prefer:
1
>>> name = "alice"
2
>>> logging.debug("User name: %s", name)
Copied!
Over:
1
>>> logging.debug("User name: {}".format(name))
Copied!
Or:
1
>>> logging.debug("User name: " + name)
Copied!
Return to the Top

Formatted String Literals or f-strings (Python 3.6+)

1
>>> name = 'Elizabeth'
2
>>> f'Hello {name}!'
3
'Hello Elizabeth!
Copied!
It is even possible to do inline arithmetic with it:
1
>>> a = 5
2
>>> b = 10
3
>>> f'Five plus ten is {a + b} and not {2 * (a + b)}.'
4
'Five plus ten is 15 and not 30.'
Copied!
Return to the Top

Template Strings

A simpler and less powerful mechanism, but it is recommended when handling format strings generated by users. Due to their reduced complexity template strings are a safer choice.
1
>>> from string import Template
2
>>> name = 'Elizabeth'
3
>>> t = Template('Hey $name!')
4
>>> t.substitute(name=name)
5
'Hey Elizabeth!'
Copied!
Return to the Top

Regular Expressions

  1. 1.
    Import the regex module with import re.
  2. 2.
    Create a Regex object with the re.compile() function. (Remember to use a raw string.)
  3. 3.
    Pass the string you want to search into the Regex object’s search() method. This returns a Match object.
  4. 4.
    Call the Match object’s group() method to return a string of the actual matched text.
All the regex functions in Python are in the re module:
1
>>> import re
Copied!
Return to the Top

Matching Regex Objects

1
>>> phone_num_regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
2
3
>>> mo = phone_num_regex.search('My number is 415-555-4242.')
4
5
>>> print('Phone number found: {}'.format(mo.group()))
6
Phone number found: 415-555-4242
Copied!
Return to the Top

Grouping with Parentheses

1
>>> phone_num_regex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
2
3
>>> mo = phone_num_regex.search('My number is 415-555-4242.')
4
5
>>> mo.group(1)
6
'415'
7
8
>>> mo.group(2)
9
'555-4242'
10
11
>>> mo.group(0)
12
'415-555-4242'
13
14
>>> mo.group()
15
'415-555-4242'
Copied!
To retrieve all the groups at once: use the groups() method—note the plural form for the name.
1
>>> mo.groups()
2
('415', '555-4242')
3
4
>>> area_code, main_number = mo.groups()
5
6
>>> print(area_code)
7
415
8
9
>>> print(main_number)
10
555-4242
Copied!
Return to the Top

Matching Multiple Groups with the Pipe

The | character is called a pipe. You can use it anywhere you want to match one of many expressions. For example, the regular expression r'Batman|Tina Fey' will match either 'Batman' or 'Tina Fey'.
1
>>> hero_regex = re.compile (r'Batman|Tina Fey')
2
3
>>> mo1 = hero_regex.search('Batman and Tina Fey.')
4
5
>>> mo1.group()
6
'Batman'
7
8
>>> mo2 = hero_regex.search('Tina Fey and Batman.')
9
10
>>> mo2.group()
11
'Tina Fey'
Copied!
You can also use the pipe to match one of several patterns as part of your regex:
1
>>> bat_regex = re.compile(r'Bat(man|mobile|copter|bat)')
2
3
>>> mo = bat_regex.search('Batmobile lost a wheel')
4
5
>>> mo.group()
6
'Batmobile'
7
8
>>> mo.group(1)
9
'mobile'
Copied!
Return to the Top

Optional Matching with the Question Mark

The ? character flags the group that precedes it as an optional part of the pattern.
1
>>> bat_regex = re.compile(r'Bat(wo)?man')
2
>>> mo1 = bat_regex.search('The Adventures of Batman')
3
>>> mo1.group()
4
'Batman'
5
6
>>> mo2 = bat_regex.search('The Adventures of Batwoman')
7
>>> mo2.group()
8
'Batwoman'
Copied!
Return to the Top

Matching Zero or More with the Star

The * (called the star or asterisk) means “match zero or more”—the group that precedes the star can occur any number of times in the text.
1
>>> bat_regex = re.compile(r'Bat(wo)*man')
2
>>> mo1 = bat_regex.search('The Adventures of Batman')
3
>>> mo1.group()
4
'Batman'
5
6
>>> mo2 = bat_regex.search('The Adventures of Batwoman')
7
>>> mo2.group()
8
'Batwoman'
9
10
>>> mo3 = bat_regex.search('The Adventures of Batwowowowoman')
11
>>> mo3.group()
12
'Batwowowowoman'
Copied!
Return to the Top

Matching One or More with the Plus

While * means “match zero or more,” the + (or plus) means “match one or more”. The group preceding a plus must appear at least once. It is not optional:
1
>>> bat_regex = re.compile(r'Bat(wo)+man')
2
>>> mo1 = bat_regex.search('The Adventures of Batwoman')
3
>>> mo1.group()
4
'Batwoman'
Copied!
1
>>> mo2 = bat_regex.search('The Adventures of Batwowowowoman')
2
>>> mo2.group()
3
'Batwowowowoman'
Copied!
1
>>> mo3 = bat_regex.search('The Adventures of Batman')
2
>>> mo3 is None
3
True
Copied!
Return to the Top

Matching Specific Repetitions with Curly Brackets

If you have a group that you want to repeat a specific number of times, follow the group in your regex with a number in curly brackets. For example, the regex (Ha){3} will match the string 'HaHaHa', but it will not match 'HaHa', since the latter has only two repeats of the (Ha) group.
Instead of one number, you can specify a range by writing a minimum, a comma, and a maximum in between the curly brackets. For example, the regex (Ha){3,5} will match 'HaHaHa', 'HaHaHaHa', and 'HaHaHaHaHa'.
1
>>> ha_regex = re.compile(r'(Ha){3}')
2
>>> mo1 = ha_regex.search('HaHaHa')
3
>>> mo1.group()
4
'HaHaHa'
Copied!
1
>>> mo2 = ha_regex.search('Ha')
2
>>> mo2 is None
3
True
Copied!
Return to the Top

Greedy and Nongreedy Matching

Python’s regular expressions are greedy by default, which means that in ambiguous situations they will match the longest string possible. The non-greedy version of the curly brackets, which matches the shortest string possible, has the closing curly bracket followed by a question mark.
1
>>> greedy_ha_regex = re.compile(r'(Ha){3,5}')
2
>>> mo1 = greedy_ha_regex.search('HaHaHaHaHa')
3
>>> mo1.group()
4
'HaHaHaHaHa'
Copied!
1
>>> nongreedy_ha_regex = re.compile(r'(Ha){3,5}?')
2
>>> mo2 = nongreedy_ha_regex.search('HaHaHaHaHa')
3
>>> mo2.group()
4
'HaHaHa'
Copied!
Return to the Top

The findall() Method

In addition to the search() method, Regex objects also have a findall() method. While search() will return a Match object of the first matched text in the searched string, the findall() method will return the strings of every match in the searched string.
1
>>> phone_num_regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups
2
3
>>> phone_num_regex.findall('Cell: 415-555-9999 Work: 212-555-0000')
4
['415-555-9999', '212-555-0000']
Copied!
To summarize what the findall() method returns, remember the following:
  • When called on a regex with no groups, such as \d-\d\d\d-\d\d\d\d, the method findall() returns a list of ng matches, such as ['415-555-9999', '212-555-0000'].
  • When called on a regex that has groups, such as (\d\d\d)-(d\d)-(\d\d\d\d), the method findall() returns a list of es of strings (one string for each group), such as [('415', '555', '9999'), ('212', '555', '0000')].
Return to the Top

Making Your Own Character Classes

There are times when you want to match a set of characters but the shorthand character classes (\d, \w, \s, and so on) are too broad. You can define your own character class using square brackets. For example, the character class [aeiouAEIOU] will match any vowel, both lowercase and uppercase.
1
>>> vowel_regex = re.compile(r'[aeiouAEIOU]')
2
3
>>> vowel_regex.findall('Robocop eats baby food. BABY FOOD.')
4
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']
Copied!
You can also include ranges of letters or numbers by using a hyphen. For example, the character class [a-zA-Z0-9] will match all lowercase letters, uppercase letters, and numbers.
By placing a caret character (^) just after the character class’s opening bracket, you can make a negative character class. A negative character class will match all the characters that are not in the character class. For example, enter the following into the interactive shell:
1
>>> consonant_regex = re.compile(r'[^aeiouAEIOU]')
2
3
>>> consonant_regex.findall('Robocop eats baby food. BABY FOOD.')
4
['R', 'b', 'c', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', '
5
', 'B', 'B', 'Y', ' ', 'F', 'D', '.']
Copied!
Return to the Top

The Caret and Dollar Sign Characters

  • You can also use the caret symbol (^) at the start of a regex to indicate that a match must occur at the beginning of the searched text.
  • Likewise, you can put a dollar sign ($) at the end of the regex to indicate the string must end with this regex pattern.
  • And you can use the ^ and $ together to indicate that the entire string must match the regex—that is, it’s not enough for a match to be made on some subset of the string.
The r'^Hello' regular expression string matches strings that begin with 'Hello':
1
>>> begins_with_hello = re.compile(r'^Hello')
2
3
>>> begins_with_hello.search('Hello world!')
4
<_sre.SRE_Match object; span=(0, 5), match='Hello'>
5
6
>>> begins_with_hello.search('He said hello.') is None
7
True
Copied!
The r'\d#x27; regular expression string matches strings that end with a numeric character from 0 to 9:
1
>>> whole_string_is_num = re.compile(r'^\d+#x27;)
2
3
>>> whole_string_is_num.search('1234567890')
4
<_sre.SRE_Match object; span=(0, 10), match='1234567890'>
5
6
>>> whole_string_is_num.search('12345xyz67890') is None
7
True
8
9
>>> whole_string_is_num.search('12 34567890') is None
10
True
Copied!
Return to the Top

The Wildcard Character

The . (or dot) character in a regular expression is called a wildcard and will match any character except for a newline:
1
>>> at_regex = re.compile(r'.at')
2
3
>>> at_regex.findall('The cat in the hat sat on the flat mat.')
4
['cat', 'hat', 'sat', 'lat', 'mat']
Copied!
Return to the Top

Matching Everything with Dot-Star

1
>>> name_regex = re.compile(r'First Name: (.*) Last Name: (.*)')
2
3
>>> mo = name_regex.search('First Name: Al Last Name: Sweigart')
4
5
>>> mo.group(1)
6
'Al'
Copied!
1
>>> mo.group(2)
2
'Sweigart'
Copied!
The dot-star uses greedy mode: It will always try to match as much text as possible. To match any and all text in a nongreedy fashion, use the dot, star, and question mark (.*?). The question mark tells Python to match in a nongreedy way:
1
>>> nongreedy_regex = re.compile(r'<.*?>')
2
>>> mo = nongreedy_regex.search('<To serve man> for dinner.>')
3
>>> mo.group()
4
'<To serve man>'
Copied!
1
>>> greedy_regex = re.compile(r'<.*>')
2
>>> mo = greedy_regex.search('<To serve man> for dinner.>')
3
>>> mo.group()
4
'<To serve man> for dinner.>'
Copied!
Return to the Top

Matching Newlines with the Dot Character

The dot-star will match everything except a newline. By passing re.DOTALL as the second argument to re.compile(), you can make the dot character match all characters, including the newline character:
1
>>> no_newline_regex = re.compile('.*')
2
>>> no_newline_regex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()
3
'Serve the public trust.'
Copied!
1
>>> newline_regex = re.compile('.*', re.DOTALL)
2
>>> newline_regex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()
3
'Serve the public trust.\nProtect the innocent.\nUphold the law.'
Copied!
Return to the Top

Review of Regex Symbols

Symbol
Matches
?
zero or one of the preceding group.
*
zero or more of the preceding group.
+
one or more of the preceding group.
{n}
exactly n of the preceding group.
{n,}
n or more of the preceding group.
{,m}
0 to m of the preceding group.
{n,m}
at least n and at most m of the preceding p.
{n,m}? or *? or +?
performs a nongreedy match of the preceding p.
^spam
means the string must begin with spam.
spam$
means the string must end with spam.
.
any character, except newline characters.
\d, \w, and \s
a digit, word, or space character, respectively.
\D, \W, and \S
anything except a digit, word, or space, respectively.
[abc]
any character between the brackets (such as a, b, ).
[^abc]
any character that isn’t between the brackets.
Return to the Top

Case-Insensitive Matching

To make your regex case-insensitive, you can pass re.IGNORECASE or re.I as a second argument to re.compile():
1
>>> robocop = re.compile(r'robocop', re.I)
2
3
>>> robocop.search('Robocop is part man, part machine, all cop.').group()
4
'Robocop'
Copied!
1
>>> robocop.search('ROBOCOP protects the innocent.').group()
2
'ROBOCOP'
Copied!
1
>>> robocop.search('Al, why does your programming book talk about robocop so much?').group()
2
'robocop'
Copied!
Return to the Top

Substituting Strings with the sub() Method

The sub() method for Regex objects is passed two arguments:
  1. 1.
    The first argument is a string to replace any matches.
  2. 2.
    The second is the string for the regular expression.
The sub() method returns a string with the substitutions applied:
1
>>> names_regex = re.compile(r'Agent \w+')
2
3
>>> names_regex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.')
4
'CENSORED gave the secret documents to CENSORED.'
Copied!
Another example:
1
>>> agent_names_regex = re.compile(r'Agent (\w)\w*')
2
3
>>> agent_names_regex.sub(r'\1****', 'Agent Alice told Agent Carol that Agent Eve knew Agent Bob was a double agent.')
4
A**** told C**** that E**** knew B**** was a double agent.'
Copied!
Return to the Top

Managing Complex Regexes

To tell the re.compile() function to ignore whitespace and comments inside the regular expression string, “verbose mode” can be enabled by passing the variable re.VERBOSE as the second argument to re.compile().
Now instead of a hard-to-read regular expression like this:
1
phone_regex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?)')
Copied!
you can spread the regular expression over multiple lines with comments like this:
1
phone_regex = re.compile(r'''(
2
(\d{3}|\(\d{3}\))? # area code
3
(\s|-|\.)? # separator
4
\d{3} # first 3 digits
5
(\s|-|\.) # separator
6
\d{4} # last 4 digits
7
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
8
)''', re.VERBOSE)
Copied!
Return to the Top

Handling File and Directory Paths

There are two main modules in Python that deals with path manipulation. One is the os.path module and the other is the pathlib module. The pathlib module was added in Python 3.4, offering an object-oriented way to handle file system paths.
Return to the Top

Backslash on Windows and Forward Slash on OS X and Linux

On Windows, paths are written using backslashes (\) as the separator between folder names. On Unix based operating system such as macOS, Linux, and BSDs, the forward slash (/) is used as the path separator. Joining paths can be a headache if your code needs to work on different platforms.
Fortunately, Python provides easy ways to handle this. We will showcase how to deal with this with both os.path.join and pathlib.Path.joinpath
Using os.path.join on Windows:
1
>>> import os
2
3
>>> os.path.join('usr', 'bin', 'spam')
4
'usr\\bin\\spam'
Copied!
And using pathlib on *nix:
1
>>> from pathlib import Path
2
3
>>> print(Path('usr').joinpath('bin').joinpath('spam'))
4
usr/bin/spam
Copied!
pathlib also provides a shortcut to joinpath using the / operator:
1
>>> from pathlib import Path
2
3
>>> print(Path('usr') / 'bin' / 'spam')
4
usr/bin/spam
Copied!
Notice the path separator is different between Windows and Unix based operating system, that's why you want to use one of the above methods instead of adding strings together to join paths together.
Joining paths is helpful if you need to create different file paths under the same directory.
Using os.path.join on Windows:
1
>>> my_files = ['accounts.txt', 'details.csv', 'invite.docx']
2
3
>>> for filename in my_files:
4
>>> print(os.path.join('C:\\Users\\asweigart', filename))
5
C:\Users\asweigart\accounts.txt
6
C:\Users\asweigart\details.csv
7
C:\Users\asweigart\invite.docx
Copied!
Using pathlib on *nix:
1
>>> my_files = ['accounts.txt', 'details.csv', 'invite.docx']
2
>>> home = Path.home()
3
>>> for filename in my_files:
4
>>> print(home / filename)
5
/home/asweigart/accounts.txt
6
/home/asweigart/details.csv
7
/home/asweigart/invite.docx
Copied!
Return to the Top

The Current Working Directory

Using os on Windows:
1
>>> import os
2
3
>>> os.getcwd()
4
'C:\\Python34'
5
>>> os.chdir('C:\\Windows\\System32')
6
7
>>> os.getcwd()
8
'C:\\Windows\\System32'
Copied!
Using pathlib on *nix:
1
>>> from pathlib import Path
2
>>> from os import chdir
3
4
>>> print(Path.cwd())
5
/home/asweigart
6
7
>>> chdir('/usr/lib/python3.6')
8
>>> print(Path.cwd())
9
/usr/lib/python3.6
Copied!
Return to the Top

Creating New Folders

Using os on Windows:
1
>>> import os
2
>>> os.makedirs('C:\\delicious\\walnut\\waffles')
Copied!
Using pathlib on *nix:
1
>>> from pathlib import Path
2
>>> cwd = Path.cwd()
3
>>> (cwd / 'delicious' / 'walnut' / 'waffles').mkdir()
4
Traceback (most recent call last):
5
File "<stdin>", line 1, in <module>
6
File "/usr/lib/python3.6/pathlib.py", line 1226, in mkdir
7
self._accessor.mkdir(self, mode)
8
File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
9
return strfunc(str(pathobj), *args)
10
FileNotFoundError: [Errno 2] No such file or directory: '/home/asweigart/delicious/walnut/waffles'
Copied!
Oh no, we got a nasty error! The reason is that the 'delicious' directory does not exist, so we cannot make the 'walnut' and the 'waffles' directories under it. To fix this, do:
1
>>> from pathlib import Path
2
>>> cwd = Path.cwd()
3
>>> (cwd / 'delicious' / 'walnut' / 'waffles').mkdir(parents=True)
Copied!
And all is good :)
Return to the Top

Absolute vs. Relative Paths

There are two ways to specify a file path.
  • An absolute path, which always begins with the root folder
  • A relative path, which is relative to the program’s current working directory
There are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that can be used in a path. A single period (“dot”) for a folder name is shorthand for “this directory.” Two periods (“dot-dot”) means “the parent folder.”
Return to the Top

Handling Absolute and Relative Paths

To see if a path is an absolute path:
Using os.path on *nix:
1
>>> import os
2
>>> os.path.isabs('/')
3
True
4
>>> os.path.isabs('..')
5
False
Copied!
Using pathlib on *nix:
1
>>> from pathlib import Path
2
>>> Path('/').is_absolute()
3
True
4
>>> Path('..').is_absolute()
5
False
Copied!
You can extract an absolute path with both os.path and pathlib
Using os.path on *nix:
1
>>> import os
2
>>> os.getcwd()
3
'/home/asweigart'
4
>>> os.path.abspath('..')
5
'/home'
Copied!
Using pathlib on *nix:
1
from pathlib import Path
2
print(Path.cwd())
3
/home/asweigart
4
print(Path('..').resolve())
5
/home
Copied!
You can get a relative path from a starting path to another path.
Using os.path on *nix:
1
>>> import os
2
>>> os.path.relpath('/etc/passwd', '/')
3
'etc/passwd'
Copied!
Using pathlib on *nix:
1
>>> from pathlib import Path
2
>>> print(Path('/etc/passwd').relative_to('/'))
3
etc/passwd
Copied!
Return to the Top

Checking Path Validity

Checking if a file/directory exists:
Using os.path on *nix:
1
import os
2
>>> os.path.exists('.')
3
True
4
>>> os.path.exists('setup.py')
5
True
6
>>> os.path.exists('/etc')
7
True
8
>>> os.path.exists('nonexistentfile')
9
False
Copied!
Using pathlib on *nix:
1
from pathlib import Path
2
>>> Path('.').exists()
3
True
4
>>> Path('setup.py').exists()
5
True
6
>>> Path('/etc').exists()
7
True
8
>>> Path('nonexistentfile').exists()
9
False
Copied!
Checking if a path is a file:
Using os.path on *nix:
1
>>> import os
2
>>> os.path.isfile('setup.py')
3
True
4
>>> os.path.isfile('/home')
5
False
6
>>> os.path.isfile('nonexistentfile')
7
False
Copied!
Using pathlib on *nix:
1
>>> from pathlib import Path
2
>>> Path('setup.py').is_file()
3
True
4
>>> Path('/home').is_file()
5
False
6
>>> Path('nonexistentfile').is_file()
7
False
Copied!
Checking if a path is a directory:
Using os.path on *nix:
1
>>> import os
2
>>> os.path.isdir('/')
3
True
4
>>> os.path.isdir('setup.py')
5
False
6
>>> os.path.isdir('/spam')
7
False
Copied!
Using pathlib on *nix:
1
>>> from pathlib import Path
2
>>> Path('/').is_dir()
3
True
4
>>> Path('setup.py').is_dir()
5
False
6
>>> Path('/spam').is_dir()
7
False
Copied!
Return to the Top

Finding File Sizes and Folder Contents

Getting a file's size in bytes:
Using os.path on Windows:
1
>>> import os
2
>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')
3
776192
Copied!
Using pathlib on *nix:
1
>>> from pathlib import Path
2
>>> stat = Path('/bin/python3.6').stat()
3
>>> print(stat) # stat contains some other information about the file as well
4
os.stat_result(st_mode=33261, st_ino=141087, st_dev=2051, st_nlink=2, st_uid=0,
5
--snip--
6
st_gid=0, st_size=10024, st_atime=1517725562, st_mtime=1515119809, st_ctime=1517261276)
7
>>> print(stat.st_size) # size in bytes
8
10024
Copied!
Listing directory contents using os.listdir on Windows:
1
>>> import os
2
>>> os.listdir('C:\\Windows\\System32')
3
['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',
4
--snip--
5
'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']
Copied!
Listing directory contents using pathlib on *nix:
1
>>> from pathlib import Path
2
>>> for f in Path('/usr/bin').iterdir():
3
>>> print(f)
4
...
5
/usr/bin/tiff2rgba
6
/usr/bin/iconv
7
/usr/bin/ldd
8
/usr/bin/cache_restore
9
/usr/bin/udiskie
10
/usr/bin/unix2dos
11
/usr/bin/t1reencode
12
/usr/bin/epstopdf
13
/usr/bin/idle3
14
...
Copied!
To find the total size of all the files in this directory:
WARNING: Directories themselves also have a size! So you might want to check for whether a path is a file or directory using the methods in the methods discussed in the above section!
Using os.path.getsize() and os.listdir() together on Windows:
1
>>> import os
2
>>> total_size = 0
3
4
>>> for filename in os.listdir('C:\\Windows\\System32'):
5
total_size = total_size + os.path.getsize(os.path.join('C:\\Windows\\System32', filename))
6
7
>>> print(total_size)
8
1117846456
Copied!
Using pathlib on *nix:
1
>>> from pathlib import Path
2
>>> total_size = 0
3
4
>>> for sub_path in Path('/usr/bin').iterdir():
5
... total_size += sub_path.stat().st_size
6
>>>
7
>>> print(total_size)
8
1903178911
Copied!
Return to the Top

Copying Files and Folders

The shutil module provides functions for copying files, as well as entire folders.
1
>>> import shutil, os
2
3
>>> os.chdir('C:\\')
4
5
>>> shutil.copy('C:\\spam.txt', 'C:\\delicious')
6
'C:\\delicious\\spam.txt'
7
8
>>> shutil.copy('eggs.txt', 'C:\\delicious\\eggs2.txt')
9
'C:\\delicious\\eggs2.txt'
Copied!
While shutil.copy() will copy a single file, shutil.copytree() will copy an entire folder and every folder and file contained in it:
1
>>> import shutil, os
2
3
>>> os.chdir('C:\\')
4
5
>>> shutil.copytree('C:\\bacon', 'C:\\bacon_backup')
6
'C:\\bacon_backup'
Copied!
Return to the Top

Moving and Renaming Files and Folders

1
>>> import shutil
2
>>> shutil.move('C:\\bacon.txt', 'C:\\eggs')
3
'C:\\eggs\\bacon.txt'
Copied!
The destination path can also specify a filename. In the following example, the source file is moved and renamed:
1
>>> shutil.move('C:\\bacon.txt', 'C:\\eggs\\new_bacon.txt')
2
'C:\\eggs\\new_bacon.txt'
Copied!
If there is no eggs folder, then move() will rename bacon.txt to a file named eggs.
1
>>> shutil.move('C:\\bacon.txt', 'C:\\eggs')
2
'C:\\eggs'
Copied!
Return to the Top

Permanently Deleting Files and Folders

  • Calling os.unlink(path) or Path.unlink() will delete the file at path.
  • Calling os.rmdir(path) or Path.rmdir() will delete the folder at path. This folder must be empty of any files or folders.
  • Calling shutil.rmtree(path) will remove the folder at path, and all files and folders it contains will also be deleted.
Return to the Top

Safe Deletes with the send2trash Module

You can install this module by running pip install send2trash from a Terminal window.
1
>>> import send2trash
2
3
>>> with open('bacon.txt', 'a') as bacon_file: # creates the file
4
... bacon_file.write('Bacon is not a vegetable.')
5
25
6
7
>>> send2trash.send2trash('bacon.txt')
Copied!
Return to the Top

Walking a Directory Tree

1
>>> import os
2
>>>
3
>>> for folder_name, subfolders, filenames in os.walk('C:\\delicious'):
4
>>> print('The current folder is {}'.format(folder_name))
5
>>>
6
>>> for subfolder in subfolders:
7
>>> print('SUBFOLDER OF {}: {}'.format(folder_name, subfolder))
8
>>> for filename in filenames:
9
>>> print('FILE INSIDE {}: {}'.format(folder_name, filename))
10
>>>
11
>>> print('')
12
The current folder is C:\delicious
13
SUBFOLDER OF C:\delicious: cats
14
SUBFOLDER OF C:\delicious: walnut
15
FILE INSIDE C:\delicious: spam.txt
16
17
The current folder is C:\delicious\cats
18
FILE INSIDE C:\delicious\cats: catnames.txt
19
FILE INSIDE C:\delicious\cats: zophie.jpg
20
21
The current folder is C:\delicious\walnut
22
SUBFOLDER OF C:\delicious\walnut: waffles
23
24
The current folder is C:\delicious\walnut\waffles
25
FILE INSIDE C:\delicious\walnut\waffles: butter.txt
Copied!
Return to the Top
pathlib provides a lot more functionality than the ones listed above, like getting file name, getting file extension, reading/writing a file without manually opening it, etc. Check out the official documentation if you want to know more!

Reading and Writing Files

The File Reading/Writing Process

To read/write to a file in Python, you will want to use the with statement, which will close the file for you after you are done.
Return to the Top

Opening and reading files with the open() function

1
>>> with open('C:\\Users\\your_home_folder\\hello.txt') as hello_file:
2
... hello_content = hello_file.read()
3
>>> hello_content
4
'Hello World!'
5
6
>>> # Alternatively, you can use the *readlines()* method to get a list of string values from the file, one string for each line of text:
7
8
>>> with open('sonnet29.txt') as sonnet_file:
9
... sonnet_file.readlines()
10
[When, in disgrace with fortune and men's eyes,\n', ' I all alone beweep my
11
outcast state,\n', And trouble deaf heaven with my bootless cries,\n', And
12
look upon myself and curse my fate,']
13
14
>>> # You can also iterate through the file line by line:
15
>>> with open('sonnet29.txt') as sonnet_file:
16
... for line in sonnet_file: # note the new line character will be included in the line
17
... print(line, end='')
18
19
When, in disgrace with fortune and men's eyes,
20
I all alone beweep my outcast state,
21
And trouble deaf heaven with my bootless cries,
22
And look upon myself and curse my fate,
Copied!
Return to the Top

Writing to Files

1
>>> with open('bacon.txt', 'w') as bacon_file:
2
... bacon_file.write('Hello world!\n')
3
13
4
5
>>> with open('bacon.txt', 'a') as bacon_file:
6
... bacon_file.write('Bacon is not a vegetable.')
7
25
8
9
>>> with open('bacon.txt') as bacon_file:
10
... content = bacon_file.read()
11
12
>>> print(content)
13
Hello world!
14
Bacon is not a vegetable.
Copied!
Return to the Top

Saving Variables with the shelve Module

To save variables:
1
>>> import shelve
2
3
>>> cats = ['Zophie', 'Pooka', 'Simon']
4
>>> with shelve.open('mydata') as shelf_file:
5
... shelf_file['cats'] = cats
Copied!
To open and read variables:
1
>>> with shelve.open('mydata') as shelf_file:
2
... print(type(shelf_file))
3
... print(shelf_file['cats'])
4
<class 'shelve.DbfilenameShelf'>
5
['Zophie', 'Pooka', 'Simon']
Copied!
Just like dictionaries, shelf values have keys() and values() methods that will return list-like values of the keys and values in the shelf. Since these methods return list-like values instead of true lists, you should pass them to the list() function to get them in list form.
1
>>> with shelve.open('mydata') as shelf_file:
2
... print(list(shelf_file.keys()))
3
... print(list(shelf_file.values()))
4
['cats']
5
[['Zophie', 'Pooka', 'Simon']]
Copied!
Return to the Top

Saving Variables with the pprint.pformat() Function

1
>>> import pprint
2
3
>>> cats = [{'name': 'Zophie', 'desc': 'chubby'}, {'name': 'Pooka', 'desc': 'fluffy'}]
4
5
>>> pprint.pformat(cats)
6
"[{'desc': 'chubby', 'name': 'Zophie'}, {'desc': 'fluffy', 'name': 'Pooka'}]"
7
8
>>> with open('myCats.py', 'w') as file_obj:
9
... file_obj.write('cats = {}\n'.format(pprint.pformat(cats)))
10
83
Copied!
Return to the Top

Reading ZIP Files

1
>>> import zipfile, os
2
3
>>> os.chdir('C:\\') # move to the folder with example.zip
4
>>> with zipfile.ZipFile('example.zip') as example_zip:
5
... print(example_zip.namelist())
6
... spam_info = example_zip.getinfo('spam.txt')
7
... print(spam_info.file_size)
8
... print(spam_info.compress_size)
9
... print('Compressed file is %sx smaller!' % (round(spam_info.file_size / spam_info.compress_size, 2)))
10
11
['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg']
12
13908
13
3828
14
'Compressed file is 3.63x smaller!'
Copied!
Return to the Top

Extracting from ZIP Files

The extractall() method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory.
1
>>> import zipfile, os
2
3
>>> os.chdir('C:\\') # move to the folder with example.zip
4
5
>>> with zipfile.ZipFile('example.zip') as example_zip:
6
... example_zip.extractall()
Copied!
The extract() method for ZipFile objects will extract a single file from the ZIP file. Continue the interactive shell example:
1
>>> with zipfile.ZipFile('example.zip') as example_zip:
2
... print(example_zip.extract('spam.txt'))
3
... print(example_zip.extract('spam.txt', 'C:\\some\\new\\folders'))
4
'C:\\spam.txt'
5
'C:\\some\\new\\folders\\spam.txt'
Copied!
Return to the Top

Creating and Adding to ZIP Files

1
>>> import zipfile
2
3
>>> with zipfile.ZipFile('new.zip', 'w') as new_zip:
4
... new_zip.write('spam.txt', compress_type=zipfile.ZIP_DEFLATED)
Copied!
This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt.
Return to the Top

JSON, YAML and configuration files

JSON

Open a JSON file with:
1
import json
2
with open("filename.json", "r") as f:
3
content = json.loads(f.read())
Copied!
Write a JSON file with:
1
import json
2
3
content = {"name": "Joe", "age": 20}
4
with open("filename.json", "w") as f:
5
f.write(json.dumps(content, indent=2))
Copied!
Return to the Top

YAML

Compared to JSON, YAML allows for much better human maintainability and gives you the option to add comments. It is a convenient choice for configuration files where humans will have to edit it.
There are two main libraries allowing to access to YAML files:
Install them using pip install in your virtual environment.
The first one it easier to use but the second one, Ruamel, implements much better the YAML specification, and allow for example to modify a YAML content without altering comments.
Open a YAML file with:
1
from ruamel.yaml import YAML
2
3
with open("filename.yaml") as f:
4
yaml=YAML()
5
yaml.load(f)
Copied!
Return to the Top

Anyconfig

Anyconfig is a very handy package allowing to abstract completely the underlying configuration file format. It allows to load a Python dictionary from JSON, YAML, TOML, and so on.
Install it with:
1
pip install anyconfig
Copied!
Usage:
1
import anyconfig
2
3
conf1 = anyconfig.load("/path/to/foo/conf.d/a.yml")
Copied!
Return to the Top

Debugging

Raising Exceptions

Exceptions are raised with a raise statement. In code, a raise statement consists of the following:
  • The raise keyword
  • A call to the Exception() function
  • A string with a helpful error message passed to the Exception() function
1
>>> raise Exception('This is the error message.')
2
Traceback (most recent call last):
3
File "<pyshell#191>", line 1, in <module>
4
raise Exception('This is the error message.')
5
Exception: This is the error message.
Copied!
Often it’s the code that calls the function, not the function itself, that knows how to handle an exception. So you will commonly see a raise statement inside a function and the try and except statements in the code calling the function.
1
def box_print(symbol, width, height):
2
if len(symbol) != 1:
3
raise Exception('Symbol must be a single character string.')
4
if width <= 2:
5
raise Exception('Width must be greater than 2.')
6
if height <= 2:
7
raise Exception('Height must be greater than 2.')
8
print(symbol * width)
9
for i in range(height - 2):
10
print(symbol + (' ' * (width - 2)) + symbol)
11
print(symbol * width)
12
for sym, w, h in (('*', 4, 4), ('O', 20, 5), ('x', 1, 3), ('ZZ', 3, 3)):
13
try:
14
box_print(sym, w, h)
15
except Exception as err:
16
print('An exception happened: ' + str(err))
Copied!
Return to the Top

Getting the Traceback as a String

The traceback is displayed by Python whenever a raised exception goes unhandled. But can also obtain it as a string by calling traceback.format_exc(). This function is useful if you want the information from an exception’s traceback but also want an except statement to gracefully handle the exception. You will need to import Python’s traceback module before calling this function.
1
>>> import traceback
2
3
>>> try:
4
>>> raise Exception('This is the error message.')
5
>>> except:
6
>>> with open('errorInfo.txt', 'w') as error_file:
7
>>> error_file.write(traceback.format_exc())
8
>>> print('The traceback info was written to errorInfo.txt.')
9
116
10
The traceback info was written to errorInfo.txt.
Copied!
The 116 is the return value from the write() method, since 116 characters were written to the file. The traceback text was written to errorInfo.txt.
1
Traceback (most recent call last):
2
File "<pyshell#28>", line 2, in <module>
3
Exception: This is the error message.
Copied!
Return to the Top

Assertions

An assertion is a sanity check to make sure your code isn’t doing something obviously wrong. These sanity checks are performed by assert statements. If the sanity check fails, then an AssertionError exception is raised. In code, an assert statement consists of the following:
  • The assert keyword
  • A condition (that is, an expression that evaluates to True or False)
  • A comma
  • A string to display when the condition is False
1
>>> pod_bay_door_status = 'open'
2
3
>>> assert pod_bay_door_status == 'open', 'The pod bay doors need to be "open".'
4
5
>>> pod_bay_door_status = 'I\'m sorry, Dave. I\'m afraid I can\'t do that.'
6
7
>>> assert pod_bay_door_status == 'open', 'The pod bay doors need to be "open".'
8
9
Traceback (most recent call last):
10
File "<pyshell#10>", line 1, in <module>
11
assert pod_bay_door_status == 'open', 'The pod bay doors need to be "open".'
12
AssertionError: The pod bay doors need to be "open".
Copied!
In plain English, an assert statement says, “I assert that this condition holds true, and if not, there is a bug somewhere in the program.” Unlike exceptions, your code should not handle assert statements with try and except; if an assert fails, your program should crash. By failing fast like this, you shorten the time between the original cause of the bug and when you first notice the bug. This will reduce the amount of code you will have to check before finding the code that’s causing the bug.
Disabling Assertions
Assertions can be disabled by passing the -O option when running Python.
Return to the Top

Logging

To enable the logging module to display log messages on your screen as your program runs, copy the following to the top of your program (but under the #! python shebang line):
1
import logging
2
3
logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s')
Copied!
Say you wrote a function to calculate the factorial of a number. In mathematics, factorial 4 is 1 × 2 × 3 × 4, or 24. Factorial 7 is 1 × 2 × 3 × 4 × 5 × 6 × 7, or 5,040. Open a new file editor window and enter the following code. It has a bug in it, but you will also enter several log messages to help yourself figure out what is going wrong. Save the program as factorialLog.py.
1
>>> import logging
2
>>>
3
>>> logging.basicConfig(level=logging.DEBUG, format=' %(asctime)s - %(levelname)s- %(message)s')
4
>>>
5
>>> logging.debug('Start of program')
6
>>>
7
>>> def factorial(n):
8
>>>
9
>>> logging.debug('Start of factorial(%s)' % (n))
10
>>> total = 1
11
>>>
12
>>> for i in range(1, n + 1):
13
>>> total *= i
14
>>> logging.debug('i is ' + str(i) + ', total is ' + str(total))
15
>>>
16
>>> logging.debug('End of factorial(%s)' % (n))
17
>>>
18
>>> return total
19
>>>
20
>>> print(factorial(5))
21
>>> logging.debug('End of program')
22
2015-05-23 16:20:12,664 - DEBUG - Start of program
23
2015-05-23 16:20:12,664 - DEBUG - Start of factorial(5)
24
2015-05-23 16:20:12,665 - DEBUG - i is 0, total is 0
25
2015-05-23 16:20:12,668 - DEBUG - i is 1, total is 0
26
2015-05-23 16:20:12,670 - DEBUG - i is 2, total is 0
27
2015-05-23 16:20:12,673 - DEBUG - i is 3, total is 0
28
2015-05-23 16:20:12,675 - DEBUG - i is 4, total is 0
29
2015-05-23 16:20:12,678 - DEBUG - i is 5, total is 0
30
2015-05-23 16:20:12,680 - DEBUG - End of factorial(5)
31
0
32
2015-05-23 16:20:12,684 - DEBUG - End of program
Copied!
Return to the Top

Logging Levels

Logging levels provide a way to categorize your log messages by importance. There are five logging levels, described in Table 10-1 from least to most important. Messages can be logged at each level using a different logging function.
Level
Logging Function
Description
DEBUG
logging.debug()
The lowest level. Used for small details. Usually you care about these messages only when diagnosing problems.
INFO
logging.info()
Used to record information on general events in your program or confirm that things are working at their point in the program.
WARNING
logging.warning()
Used to indicate a potential problem that doesn’t prevent the program from working but might do so in the future.
ERROR
logging.error()
Used to record an error that caused the program to fail to do something.
CRITICAL
logging.critical()
The highest level. Used to indicate a fatal error that has caused or is about to cause the program to stop running entirely.
Return to the Top

Disabling Logging

After you’ve debugged your program, you probably don’t want all these log messages cluttering the screen. The logging.disable() function disables these so that you don’t have to go into your program and remove all the logging calls by hand.
1
>>> import logging
2
3
>>> logging.basicConfig(level=logging.INFO, format=' %(asctime)s -%(levelname)s - %(message)s')
4
5
>>> logging.critical('Critical error! Critical error!')
6
2015-05-22 11:10:48,054 - CRITICAL - Critical error! Critical error!
7
8
>>> logging.disable(logging.CRITICAL)
9
10
>>> logging.critical('Critical error! Critical error!')
11
12
>>> logging.error('Error! Error!')
Copied!
Return to the Top

Logging to a File

Instead of displaying the log messages to the screen, you can write them to a text file. The logging.basicConfig() function takes a filename keyword argument, like so:
1
import logging
2
3
logging.basicConfig(filename='myProgramLog.txt', level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
Copied!
Return to the Top

Lambda Functions

This function:
1
>>> def add(x, y):
2
return x + y
3
4
>>> add(5, 3)
5
8
Copied!
Is equivalent to the lambda function:
1
>>> add = lambda x, y: x + y
2
>>> add(5, 3)
3
8
Copied!
It's not even need to bind it to a name like add before:
1
>>> (lambda x, y: x + y)(5, 3)
2
8
Copied!
Like regular nested functions, lambdas also work as lexical closures:
1
>>> def make_adder(n):
2
return lambda x: x + n
3
4
>>> plus_3 = make_adder(3)
5
>>> plus_5 = make_adder(5)
6
7
>>> plus_3(4)
8
7
9
>>> plus_5(4)
10
9
Copied!
Note: lambda can only evaluate an expression, like a single line of code.
Return to the Top

Ternary Conditional Operator

Many programming languages have a ternary operator, which define a conditional expression. The most common usage is to make a terse simple conditional assignment statement. In other words, it offers one-line code to evaluate the first expression if the condition is true, otherwise it evaluates the second expression.
1
<expression1> if <condition> else <expression2>
Copied!
Example:
1
>>> age = 15
2
3
>>> print('kid' if age < 18 else 'adult')
4
kid
Copied!
Ternary operators can be chained:
1
>>> age = 15
2
3
>>> print('kid' if age < 13 else 'teenager' if age < 18 else 'adult')
4
teenager
Copied!
The code above is equivalent to:
1
if age < 18:
2
if age < 13:
3
print('kid')
4
else:
5
print('teenager')
6
else:
7
print('adult')
Copied!
Return to the Top

args and kwargs

The names args and kwargs are arbitrary - the important thing are the * and ** operators. They can mean:
  1. 1.
    In a function declaration, * means “pack all remaining positional arguments into a tuple named <name>”, while ** is the same for keyword arguments (except it uses a dictionary, not a tuple).
  2. 2.
    In a function call, * means “unpack tuple or list named <name> to positional arguments at this position”, while ** is the same for keyword arguments.
For example you can make a function that you can use to call any other function, no matter what parameters it has:
1
def forward(f, *args, **kwargs):
2
return f(*args, **kwargs)
Copied!
Inside forward, args is a tuple (of all positional arguments except the first one, because we specified it - the f), kwargs is a dict. Then we call f and unpack them so they become normal arguments to f.
You use *args when you have an indefinite amount of positional arguments.
1
>>> def fruits(*args):
2
>>> for fruit in args:
3
>>> print(fruit)
4
5
>>> fruits("apples", "bananas", "grapes")
6
7
"apples"
8
"bananas"
9
"grapes"
Copied!
Similarly, you use **kwargs when you have an indefinite number of keyword arguments.
1
>>> def fruit(**kwargs):
2
>>> for key, value in kwargs.items():
3
>>> print("{0}: {1}".format(key, value))
4
5
>>> fruit(name = "apple", color = "red")
6
7
name: apple
8
color: red
Copied!
1
>>> def show(arg1, arg2, *args, kwarg1=None, kwarg2=None, **kwargs):
2
>>> print(arg1)
3
>>> print(arg2)
4
>>> print(args)
5
>>> print(kwarg1)
6
>>> print(kwarg2)
7
>>> print(kwargs)
8
9
>>> data1 = [1,2,3]
10
>>> data2 = [4,5,6]
11
>>> data3 = {'a':7,'b':8,'c':9}
12
13
>>> show(*data1,*data2, kwarg1="python",kwarg2="cheatsheet",**data3)
14
1
15
2
16
(3, 4, 5, 6)
17
python
18
cheatsheet
19
{'a': 7, 'b': 8, 'c': 9}
20
21
>>> show(*data1, *data2, **data3)
22
1
23
2
24
(3, 4, 5, 6)
25
None
26
None
27
{'a': 7, 'b': 8, 'c': 9}
28
29
# If you do not specify ** for kwargs
30
>>> show(*data1, *data2, *data3)
31
1
32
2
33
(3, 4, 5, 6, "a", "b", "c")
34
None
35
None
36
{}
Copied!

Things to Remember(args)

  1. 1.
    Functions can accept a variable number of positional arguments by using *args in the def statement.
  2. 2.
    You can use the items from a sequence as the positional arguments for a function with the * operator.
  3. 3.
    Using the * operator with a generator may cause your program to run out of memory and crash.
  4. 4.
    Adding new positional parameters to functions that accept *args can introduce hard-to-find bugs.

Things to Remember(kwargs)

  1. 1.
    Function arguments can be specified by position or by keyword.
  2. 2.
    Keywords make it clear what the purpose of each argument is when it would be confusing with only positional arguments.
  3. 3.
    Keyword arguments with default values make it easy to add new behaviors to a function, especially when the function has existing callers.
  4. 4.
    Optional keyword arguments should always be passed by keyword instead of by position.
Return to the Top

Context Manager

While Python's context managers are widely used, few understand the purpose behind their use. These statements, commonly used with reading and writing files, assist the application in conserving system memory and improve resource management by ensuring specific resources are only in use for certain processes.

with statement

A context manager is an object that is notified when a context (a block of code) starts and ends. You commonly use one with the with statement. It takes care of the notifying.
For example, file objects are context managers. When a context ends, the file object is closed automatically:
1
>>> with open(filename) as f:
2
>>> file_contents = f.read()
3
4
# the open_file object has automatically been closed.
Copied!
Anything that ends execution of the block causes the context manager's exit method to be called. This includes exceptions, and can be useful when an error causes you to prematurely exit from an open file or connection. Exiting a script without properly closing files/connections is a bad idea, that may cause data loss or other problems. By using a context manager you can ensure that precautions are always taken to prevent damage or loss in this way.

Writing your own contextmanager using generator syntax

It is also possible to write a context manager using generator syntax thanks to the contextlib.contextmanager decorator:
1
>>> import contextlib
2
>>> @contextlib.contextmanager
3
... def context_manager(num):
4
... print('Enter')
5
... yield num + 1
6
... print('Exit')
7
>>> with context_manager(2) as cm:
8
... # the following instructions are run when the 'yield' point of the context
9
... # manager is reached.
10
... # 'cm' will have the value that was yielded
11
... print('Right in the middle with cm = {}'.format(cm))
12
Enter
13
Right in the middle with cm = 3
14
Exit
15
16
>>>
Copied!
Return to the Top

__main__ Top-level script environment

__main__ is the name of the scope in which top-level code executes. A module’s name is set equal to __main__ when read from standard input, a script, or from an interactive prompt.
A module can discover whether or not it is running in the main scope by checking its own __name__, which allows a common idiom for conditionally executing code in a module when it is run as a script or with python -m but not when it is imported:
1
>>> if __name__ == "__main__":
2
... # execute only if run as a script
3
... main()
Copied!
For a package, the same effect can be achieved by including a main.py module, the contents of which will be executed when the module is run with -m
For example we are developing script which is designed to be used as module, we should do:
1
>>> # Python program to execute function directly
2
>>> def add(a, b):
3
... return a+b
4
...
5
>>> add(10, 20) # we can test it by calling the function save it as calculate.py
6
30
7
>>> # Now if we want to use that module by importing we have to comment out our call,
8
>>> # Instead we can write like this in calculate.py
9
>>> if __name__ == "__main__":
10
... add(3, 5)
11
...
12
>>> import calculate
13
>>> calculate.add(3, 5)
14
8
Copied!

Advantages

  1. 1.
    Every Python module has it’s __name__ defined and if this is __main__, it implies that the module is being run standalone by the user and we can do corresponding appropriate actions.
  2. 2.
    If you import this script as a module in another script, the name is set to the name of the script/module.
  3. 3.
    Python files can act as either reusable modules, or as standalone programs.
  4. 4.
    if __name__ == “main”: is used to execute some code only if the file was run directly, and not imported.
Return to the Top

setup.py

The setup script is the centre of all activity in building, distributing, and installing modules using the Distutils. The main purpose of the setup script is to describe your module distribution to the Distutils, so that the various commands that operate on your modules do the right thing.
The setup.py file is at the heart of a Python project. It describes all of the metadata about your project. There a quite a few fields you can add to a project to give it a rich set of metadata describing the project. However, there are only three required fields: name, version, and packages. The name field must be unique if you wish to publish your package on the Python Package Index (PyPI). The version field keeps track of different releases of the project. The packages field describes where you’ve put the Python source code within your project.
This allows you to easily install Python packages. Often it's enough to write:
1
python setup.py install
Copied!
and module will install itself.
Our initial setup.py will also include information about the license and will re-use the README.txt file for the long_description field. This will look like:
1
>>> from distutils.core import setup
2
>>> setup(
3
... name='pythonCheatsheet',
4
... version='0.1',
5
... packages=['pipenv',],
6
... license='MIT',
7
... long_description=open('README.txt').read(),
8
... )
Copied!
Find more information visit http://docs.python.org/install/index.html.
Return to the Top

Dataclasses

Dataclasses are python classes but are suited for storing data objects. This module provides a decorator and functions for automatically adding generated special methods such as __init__() and __repr__() to user-defined classes.

Features

  1. 1.
    They store data and represent a certain data type. Ex: A number. For people familiar with ORMs, a model instance is a data object. It represents a specific kind of entity. It holds attributes that define or represent the entity.
  2. 2.
    They can be compared to other objects of the same type. Ex: A number can be greater than, less than, or equal to another number.
Python 3.7 provides a decorator dataclass that is used to convert a class into a dataclass.
python 2.7
1
>>> class Number:
2
... def __init__(self, val):
3
... self.val = val
4
...
5
>>> obj = Number(2)
6
>>> obj.val
7
2
Copied!
with dataclass
1
>>> @dataclass
2
... class Number:
3
... val: int
4
...
5
>>> obj = Number(2)
6
>>> obj.val
7
2
Copied!
Return to the Top

Default values

It is easy to add default values to the fields of your data class.
1
>>> @dataclass
2
... class Product:
3
... name: str
4
... count: int = 0
5
... price: float = 0.0
6
...
7
>>> obj = Product("Python")
8
>>> obj.name
9
Python
10
>>> obj.count
11
0
12
>>> obj.price
13
0.0
Copied!

Type hints

It is mandatory to define the data type in dataclass. However, If you don't want specify the datatype then, use typing.Any.
1
>>> from dataclasses import dataclass
2
>>> from typing import Any
3
4
>>> @dataclass
5
... class WithoutExplicitTypes:
6
... name: Any
7
... value: Any = 42
8
...
Copied!
Return to the Top

Virtual Environment

The use of a Virtual Environment is to test python code in encapsulated environments and to also avoid filling the base Python installation with libraries we might use for only one project.
Return to the Top

virtualenv

  1. 1.
    Install virtualenv
    1
    pip install virtualenv
    Copied!
  2. 2.
    Install virtualenvwrapper-win (Windows)
    1
    pip install virtualenvwrapper-win
    Copied!
Usage:
  1. 1.
    Make a Virtual Environment
    1
    mkvirtualenv HelloWold
    Copied!
    Anything we install now will be specific to this project. And available to the projects we connect to this environment.
  2. 2.
    Set Project Directory
    To bind our virtualenv with our current working directory we simply enter:
    1
    setprojectdir .
    Copied!
  3. 3.
    Deactivate
    To move onto something else in the command line type ‘deactivate’ to deactivate your environment.
    1
    deactivate
    Copied!
    Notice how the parenthesis disappear.
  4. 4.
    Workon
    Open up the command prompt and type ‘workon HelloWold’ to activate the environment and move into your root project folder
    1
    workon HelloWold
    Copied!
Return to the Top

poetry

Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.
  1. 1.
    Install Poetry
    1
    pip install --user poetry
    Copied!
  2. 2.
    Create a new project
    1
    poetry new my-project
    Copied!
    This will create a my-project directory:
    1
    my-project
    2
    ├── pyproject.toml
    3
    ├── README.rst
    4
    ├── poetry_demo
    5
    │ └── __init__.py
    6
    └── tests
    7
    ├── __init__.py
    8
    └── test_poetry_demo.py
    Copied!
    The pyproject.toml file will orchestrate your project and its dependencies:
    1
    [tool.poetry]
    2
    name = "my-project"
    3
    version = "0.1.0"
    4
    description = ""
    5
    authors = ["your name <[email protected]>"]
    6
    7
    [tool.poetry.dependencies]
    8
    python = "*"
    9
    10
    [tool.poetry.dev-dependencies]
    11
    pytest = "^3.4"
    Copied!
  3. 3.
    Packages
    To add dependencies to your project, you can specify them in the tool.poetry.dependencies section:
    1
    [tool.poetry.dependencies]
    2
    pendulum = "^1.4"
    Copied!
    Also, instead of modifying the pyproject.toml file by hand, you can use the add command and it will automatically find a suitable version constraint.
    1
    $ poetry add pendulum
    Copied!
    To install the dependencies listed in the pyproject.toml:
    1
    poetry install
    Copied!
    To remove dependencies:
    1
    poetry remove pendulum
    Copied!
For more information, check the documentation.
Return to the Top

pipenv

Pipenv is a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world. Windows is a first-class citizen, in our world.
  1. 1.
    Install pipenv
    1
    pip install pipenv
    Copied!
  2. 2.
    Enter your Project directory and install the Packages for your project
    1
    cd my_project
    2
    pipenv install <package>
    Copied!
    Pipenv will install your package and create a Pipfile for you in your project’s directory. The Pipfile is used to track which dependencies your project needs in case you need to re-install them.
  3. 3.
    Uninstall Packages
    1
    pipenv uninstall <package>
    Copied!
  4. 4.
    Activate the Virtual Environment associated with your Python project
    1
    pipenv shell
    Copied!
  5. 5.
    Exit the Virtual Environment
    1
    exit
    Copied!
Find more information and a video in docs.pipenv.org.
Return to the Top

anaconda

Anaconda is another popular tool to manage python packages.
Where packages, notebooks, projects and environments are shared. Your place for free public conda package hosting.
Usage:
  1. 1.
    Make a Virtual Environment
    1
    conda create -n HelloWorld
    Copied!
  2. 2.
    To use the Virtual Environment, activate it by:
    1
    conda activate HelloWorld
    Copied!
    Anything installed now will be specific to the project HelloWorld
  3. 3.
    Exit the Virtual Environment
    1
    conda deactivate
    Copied!
Return to the Top
1
2
## Python Cheatsheet
3
4
- [About](#about)
5
- [Contribute](#contribute)
6
- [Read It](#read-it)
7
- [Python Cheatsheet](#python-cheatsheet)
8
- [The Zen of Python](#the-zen-of-python)
9
- [Python Basics](#python-basics)
10
- [Math Operators](#math-operators)
11
- [Data Types](#data-types)
12
- [String Concatenation and Replication](#string-concatenation-and-replication)
13
- [Variables](#variables)
14
- [Comments](#comments)
15
- [The print() Function](#the-print-function)
16
- [The input() Function](#the-input-function)
17
- [The len() Function](#the-len-function)
18
- [The str(), int(), and float() Functions](#the-str-int-and-float-functions)
19
- [Flow Control](#flow-control)
20
- [Comparison Operators](#comparison-operators)
21
- [Boolean evaluation](#boolean-evaluation)
22
- [Boolean Operators](#boolean-operators)
23
- [Mixing Boolean and Comparison Operators](#mixing-boolean-and-comparison-operators)
24
- [if Statements](#if-statements)
25
- [else Statements](#else-statements)
26
- [elif Statements](#elif-statements)
27
- [while Loop Statements](#while-loop-statements)
28
- [break Statements](#break-statements)
29
- [continue Statements](#continue-statements)
30
- [for Loops and the range() Function](#for-loops-and-the-range-function)
31
- [For else statement](#for-else-statement)
32
- [Importing Modules](#importing-modules)
33
- [Ending a Program Early with sys.exit()](#ending-a-program-early-with-sysexit)
34
- [Functions](#functions)
35
- [Return Values and return Statements](#return-values-and-return-statements)
36
- [The None Value](#the-none-value)
37
- [Keyword Arguments and print()](#keyword-arguments-and-print)
38
- [Local and Global Scope](#local-and-global-scope)
39
- [The global Statement](#the-global-statement)
40
- [Exception Handling](#exception-handling)
41
- [Basic exception handling](#basic-exception-handling)
42
- [Final code in exception handling](#final-code-in-exception-handling)
43
- [Lists](#lists)
44
- [Getting Individual Values in a List with Indexes](#getting-individual-values-in-a-list-with-indexes)
45
- [Negative Indexes](#negative-indexes)
46
- [Getting Sublists with Slices](#getting-sublists-with-slices)
47
- [Getting a List’s Length with len()](#getting-a-lists-length-with-len)
48
- [Changing Values in a List with Indexes](#changing-values-in-a-list-with-indexes)
49
- [List Concatenation and List Replication](#list-concatenation-and-list-replication)
50
- [Removing Values from Lists with del Statements](#removing-values-from-lists-with-del-statements)
51
- [Using for Loops with Lists](#using-for-loops-with-lists)
52
- [Looping Through Multiple Lists with zip()](#looping-through-multiple-lists-with-zip)
53
- [The in and not in Operators](#the-in-and-not-in-operators)
54
- [The Multiple Assignment Trick](#the-multiple-assignment-trick)
55
- [Augmented Assignment Operators](#augmented-assignment-operators)
56
- [Finding a Value in a List with the index() Method](#finding-a-value-in-a-list-with-the-index-method)
57
- [Adding Values to Lists with the append() and insert() Methods](#adding-values-to-lists-with-the-append-and-insert-methods)
58
- [Removing Values from Lists with remove()](#removing-values-from-lists-with-remove)
59
- [Removing Values from Lists with pop()](#removing-values-from-lists-with-pop)
60
- [Sorting the Values in a List with the sort() Method](#sorting-the-values-in-a-list-with-the-sort-method)
61
- [Tuple Data Type](#tuple-data-type)
62
- [Converting Types with the list() and tuple() Functions](#converting-types-with-the-list-and-tuple-functions)
63
- [Dictionaries and Structuring Data](#dictionaries-and-structuring-data)
64
- [The keys(), values(), and items() Methods](#the-keys-values-and-items-methods)
65
- [Checking Whether a Key or Value Exists in a Dictionary](#checking-whether-a-key-or-value-exists-in-a-dictionary)
66
- [The get() Method](#the-get-method)
67
- [The setdefault() Method](#the-setdefault-method)
68
- [Pretty Printing](#pretty-printing)
69
- [Merge two dictionaries](#merge-two-dictionaries)
70
- [sets](#sets)
71
- [Initializing a set](#initializing-a-set)
72
- [sets: unordered collections of unique elements](#sets-unordered-collections-of-unique-elements)
73
- [set add() and update()](#set-add-and-update)
74
- [set remove() and discard()](#set-remove-and-discard)
75
- [set union()](#set-union)
76
- [set intersection](#set-intersection)
77
- [set difference](#set-difference)
78
- [set symetric_difference](#set-symetric_difference)
79
- [itertools Module](#itertools-module)
80
- [accumulate()](#accumulate)
81
- [combinations()](#combinations)
82
- [combinations_with_replacement()](#combinations_with_replacement)
83
- [count()](#count)
84
- [cycle()](#cycle)
85
- [chain()](#chain)
86
- [compress()](#compress)
87
- [dropwhile()](#dropwhile)
88
- [filterfalse()](#filterfalse)
89
- [groupby()](#groupby)
90
- [islice()](#islice)
91
- [permutations()](#permutations)
92
- [product()](#product)
93
- [repeat()](#repeat)
94
- [starmap()](#starmap)
95
- [takewhile()](#takewhile)
96
- [tee()](#tee)
97
- [zip_longest()](#zip_longest)
98
- [Comprehensions](#comprehensions)
99
- [List comprehension](#list-comprehension)
100
- [Set comprehension](#set-comprehension)
101
- [Dict comprehension](#dict-comprehension)
102
- [Manipulating Strings](#manipulating-strings)
103
- [Escape Characters](#escape-characters)
104
- [Raw Strings](#raw-strings)
105
- [Multiline Strings with Triple Quotes](#multiline-strings-with-triple-quotes)
106
- [Indexing and Slicing Strings](#indexing-and-slicing-strings)
107
- [The in and not in Operators with Strings](#the-in-and-not-in-operators-with-strings)
108
- [The in and not in Operators with list](#the-in-and-not-in-operators-with-list)
109
- [The upper(), lower(), isupper(), and islower() String Methods](#the-upper-lower-isupper-and-islower-string-methods)
110
- [The isX String Methods](#the-isx-string-methods)
111
- [The startswith() and endswith() String Methods](#the-startswith-and-endswith-string-methods)
112
- [The join() and split() String Methods](#the-join-and-split-string-methods)
113
- [Justifying Text with rjust(), ljust(), and center()](#justifying-text-with-rjust-ljust-and-center)
114
- [Removing Whitespace with strip(), rstrip(), and lstrip()](#removing-whitespace-with-strip-rstrip-and-lstrip)
115
- [Copying and Pasting Strings with the pyperclip Module (need pip install)](#copying-and-pasting-strings-with-the-pyperclip-module-need-pip-install)
116
- [String Formatting](#string-formatting)
117
- [% operator](#-operator)
118
- [String Formatting (str.format)](#string-formatting-strformat)
119
- [Lazy string formatting](#lazy-string-formatting)
120
- [Formatted String Literals or f-strings (Python 3.6+)](#formatted-string-literals-or-f-strings-python-36)
121
- [Template Strings](#template-strings)
122
- [Regular Expressions](#regular-expressions)
123
- [Matching Regex Objects](#matching-regex-objects)
124
- [Grouping with Parentheses](#grouping-with-parentheses)
125
- [Matching Multiple Groups with the Pipe](#matching-multiple-groups-with-the-pipe)
126
- [Optional Matching with the Question Mark](#optional-matching-with-the-question-mark)
127
- [Matching Zero or More with the Star](#matching-zero-or-more-with-the-star)
128
- [Matching One or More with the Plus](#matching-one-or-more-with-the-plus)
129
- [Matching Specific Repetitions with Curly Brackets](#matching-specific-repetitions-with-curly-brackets)
130
- [Greedy and Nongreedy Matching](#greedy-and-nongreedy-matching)
131
- [The findall() Method](#the-findall-method)
132
- [Making Your Own Character Classes](#making-your-own-character-classes)
133
- [The Caret and Dollar Sign Characters](#the-caret-and-dollar-sign-characters)
134
- [The Wildcard Character](#the-wildcard-character)
135
- [Matching Everything with Dot-Star](#matching-everything-with-dot-star)
136
- [Matching Newlines with the Dot Character](#matching-newlines-with-the-dot-character)
137
- [Review of Regex Symbols](#review-of-regex-symbols)
138
- [Case-Insensitive Matching](#case-insensitive-matching)
139
- [Substituting Strings with the sub() Method](#substituting-strings-with-the-sub-method)
140
- [Managing Complex Regexes](#managing-complex-regexes)
141
- [Handling File and Directory Paths](#handling-file-and-directory-paths)
142
- [Backslash on Windows and Forward Slash on OS X and Linux](#backslash-on-windows-and-forward-slash-on-os-x-and-linux)
143
- [The Current Working Directory](#the-current-working-directory)
144
- [Creating New Folders](#creating-new-folders)
145
- [Absolute vs. Relative Paths](#absolute-vs-relative-paths)
146
- [Handling Absolute and Relative Paths](#handling-absolute-and-relative-paths)
147
- [Checking Path Validity](#checking-path-validity)
148
- [Finding File Sizes and Folder Contents](#finding-file-sizes-and-folder-contents)
149
- [Copying Files and Folders](#copying-files-and-folders)
150
- [Moving and Renaming Files and Folders](#moving-and-renaming-files-and-folders)
151
- [Permanently Deleting Files and Folders](#permanently-deleting-files-and-folders)
152
- [Safe Deletes with the send2trash Module](#safe-deletes-with-the-send2trash-module)
153
- [Walking a Directory Tree](#walking-a-directory-tree)
154
- [Reading and Writing Files](#reading-and-writing-files)
155
- [The File Reading/Writing Process](#the-file-readingwriting-process)
156
- [Opening and reading files with the open() function](#opening-and-reading-files-with-the-open-function)
157
- [Writing to Files](#writing-to-files)
158
- [Saving Variables with the shelve Module](#saving-variables-with-the-shelve-module)
159
- [Saving Variables with the pprint.pformat() Function](#saving-variables-with-the-pprintpformat-function)
160
- [Reading ZIP Files](#reading-zip-files)
161
- [Extracting from ZIP Files](#extracting-from-zip-files)
162
- [Creating and Adding to ZIP Files](#creating-and-adding-to-zip-files)
163
- [JSON, YAML and configuration files](#json-yaml-and-configuration-files)
164
- [JSON](#json)
165
- [YAML](#yaml)
166
- [Anyconfig](#anyconfig)
167
- [Debugging](#debugging)
168
- [Raising Exceptions](#raising-exceptions)
169
- [Getting the Traceback as a String](#getting-the-traceback-as-a-string)
170
- [Assertions](#assertions)
171
- [Logging](#logging)
172
- [Logging Levels](#logging-levels)
173
- [Disabling Logging](#disabling-logging)
174
- [Logging to a File](#logging-to-a-file)
175
- [Lambda Functions](#lambda-functions)
176
- [Ternary Conditional Operator](#ternary-conditional-operator)
177
- [args and kwargs](#args-and-kwargs)
178
- [Things to Remember(args)](#things-to-rememberargs)
179
- [Things to Remember(kwargs)](#things-to-rememberkwargs)
180
- [Context Manager](#context-manager)
181
- [with statement](#with-statement)
182
- [Writing your own contextmanager using generator syntax](#writing-your-own-contextmanager-using-generator-syntax)
183
- [`__main__` Top-level script environment](#__main__-top-level-script-environment)
184
- [Advantages](#advantages)
185
- [setup.py](#setuppy)
186
- [Dataclasses](#dataclasses)
187
- [Features](#features)
188
- [Default values](#default-values)
189
- [Type hints](#type-hints)
190
- [Virtual Environment](#virtual-environment)
191
- [virtualenv](#virtualenv)
192
- [poetry](#poetry)
193
- [pipenv](#pipenv)
194
- [anaconda](#anaconda)
195
196
## The Zen of Python
197
198
From the [PEP 20 -- The Zen of Python](https://www.python.org/dev/peps/pep-0020/):
199
200
> Long time Pythoneer Tim Peters succinctly channels the BDFL's guiding principles for Python's design into 20 aphorisms, only 19 of which have been written down.
201
202
```python
203
>>> import this
204
The Zen of Python, by Tim Peters
205
206
Beautiful is better than ugly.
207
Explicit is better than implicit.
208
Simple is better than complex.
209
Complex is better than complicated.
210
Flat is better than nested.
211
Sparse is better than dense.
212
Readability counts.
213
Special cases aren't special enough to break the rules.
214
Although practicality beats purity.
215
Errors should never pass silently.
216
Unless explicitly silenced.
217
In the face of ambiguity, refuse the temptation to guess.
218
There should be one-- and preferably only one --obvious way to do it.
219
Although that way may not be obvious at first unless you're Dutch.
220
Now is better than never.
221
Although never is often better than *right* now.
222
If the implementation is hard to explain, it's a bad idea.
223
If the implementation is easy to explain, it may be a good idea.
224
Namespaces are one honking great idea -- let's do more of those!
225
```
226
227
[_Return to the Top_](#python-cheatsheet)
228
229
## Python Basics
230
231
### Math Operators
232
233
From **Highest** to **Lowest** precedence:
234
235
| Operators | Operation | Example |
236
| --------- | ----------------- | --------------- |
237
| \*\* | Exponent | `2 ** 3 = 8` |
238
| % | Modulus/Remainder | `22 % 8 = 6` |
239
| // | Integer division | `22 // 8 = 2` |
240
| / | Division | `22 / 8 = 2.75` |
241
| \* | Multiplication | `3 * 3 = 9` |
242
| - | Subtraction | `5 - 2 = 3` |
243
| + | Addition | `2 + 2 = 4` |
244
245
Examples of expressions in the interactive shell:
246
247
```python
248
>>> 2 + 3 * 6
249
20
250
```
251
252
```python
253
>>> (2 + 3) * 6
254
30
255
```
256
257
```python
258
>>> 2 ** 8
259
256
260
```
261
262
```python
263
>>> 23 // 7
264
3
265
```
266
267
```python
268
>>> 23 % 7
269
2
270
```
271
272
```python
273
>>> (5 - 1) * ((7 + 1) / (3 - 1))
274
16.0
275
```
276
277
[_Return to the Top_](#python-cheatsheet)
278
279
### Data Types
280
281
| Data Type | Examples |
282
| ---------------------- | ----------------------------------------- |
283
| Integers | `-2, -1, 0, 1, 2, 3, 4, 5` |
284
| Floating-point numbers | `-1.25, -1.0, --0.5, 0.0, 0.5, 1.0, 1.25` |
285
| Strings | `'a', 'aa', 'aaa', 'Hello!', '11 cats'` |
286
287
[_Return to the Top_](#python-cheatsheet)
288
289
### String Concatenation and Replication
290
291
String concatenation:
292
293
```python
294
>>> 'Alice' 'Bob'
295
'AliceBob'
296
```
297
298
Note: Avoid `+` operator for string concatenation. Prefer string formatting.
299
300
String Replication:
301
302
```python
303
>>> 'Alice' * 5
304
'AliceAliceAliceAliceAlice'
305
```
306
307
[_Return to the Top_](#python-cheatsheet)
308
309
### Variables
310
311
You can name a variable anything as long as it obeys the following rules:
312
313
1. It can be only one word.
314
1. It can use only letters, numbers, and the underscore (`_`) character.
315
1. It can’t begin with a number.
316
1. Variable name starting with an underscore (`_`) are considered as "unuseful`.
317
318
Example:
319
320
```python
321
>>> spam = 'Hello'
322
>>> spam
323
'Hello'
324
```
325
326
```python
327
>>> _spam = 'Hello'
328
```
329
330
`_spam` should not be used again in the code.
331
332
[_Return to the Top_](#python-cheatsheet)
333
334
### Comments
335
336
Inline comment:
337
338
```python
339
# This is a comment
340
```
341
342
Multiline comment:
343
344
```Python
345
# This is a
346
# multiline comment
347
```
348
349
Code with a comment:
350
351
```python
352
a = 1 # initialization
353
```
354
355
Please note the two spaces in front of the comment.
356
357
Function docstring:
358
359
```python
360
def foo():
361
"""
362
This is a function docstring
363
You can also use:
364
''' Function Docstring '''
365
"""
366
```
367
368
[_Return to the Top_](#python-cheatsheet)
369
370
### The print() Function
371
372
```python
373
>>> print('Hello world!')
374
Hello world!
375
```
376
377
```python
378
>>> a = 1
379
>>> print('Hello world!', a)
380
Hello world! 1
381
```
382
383
[_Return to the Top_](#python-cheatsheet)
384
385
### The input() Function
386
387
Example Code:
388
389
```python
390
>>> print('What is your name?') # ask for their name
391
>>> myName = input()
392
>>> print('It is good to meet you, {}'.format(myName))
393
What is your name?
394
Al
395
It is good to meet you, Al
396
```
397
398
[_Return to the Top_](#python-cheatsheet)
399
400
### The len() Function
401
402
Evaluates to the integer value of the number of characters in a string:
403
404
```python
405
>>> len('hello')
406
5
407
```
408
409
Note: test of emptiness of strings, lists, dictionary, etc, should **not** use len, but prefer direct
410
boolean evaluation.
411
412
```python
413
>>> a = [1, 2, 3]
414
>>> if a:
415
>>> print("the list is not empty!")
416
```
417
418
[_Return to the Top_](#python-cheatsheet)
419
420
### The str(), int(), and float() Functions
421
422
Integer to String or Float:
423
424
```python
425
>>> str(29)
426
'29'
427
```
428
429
```python
430
>>> print('I am {} years old.'.format(str(29)))
431
I am 29 years old.
432
```
433
434
```python
435
>>> str(-3.14)
436
'-3.14'
437
```
438
439
Float to Integer:
440
441
```python
442
>>> int(7.7)
443
7
444
```
445
446
```python
447
>>> int(7.7) + 1
448
8
449
```
450
451
[_Return to the Top_](#python-cheatsheet)
452
453
## Flow Control
454
455
### Comparison Operators
456
457
| Operator | Meaning |
458
| -------- | ------------------------ |
459
| `==` | Equal to |
460
| `!=` | Not equal to |
461
| `<` | Less than |
462
| `>` | Greater Than |
463
| `<=` | Less than or Equal to |
464
| `>=` | Greater than or Equal to |
465
466
These operators evaluate to True or False depending on the values you give them.
467
468
Examples:
469
470
```python
471
>>> 42 == 42
472
True
473
```
474
475
```python
476
>>> 40 == 42
477
False
478
```
479
480
```python
481
>>> 'hello' == 'hello'
482
True
483
```
484
485
```python
486
>>> 'hello' == 'Hello'
487
False
488
```
489
490
```python
491
>>> 'dog' != 'cat'
492
True
493
```
494
495
```python
496
>>> 42 == 42.0
497
True
498
```
499
500
```python
501
>>> 42 == '42'
502
False
503
```
504
505
### Boolean evaluation
506
507
Never use `==` or `!=` operator to evaluate boolean operation. Use the `is` or `is not` operators,
508
or use implicit boolean evaluation.
509
510
NO (even if they are valid Python):
511
512
```python
513
>>> True == True
514
True
515
```
516
517
```python
518
>>> True != False
519
True
520
```
521
522
YES (even if they are valid Python):
523
524
```python
525
>>> True is True
526
True
527
```
528
529
```python
530
>>> True is not False
531
True
532
```
533
534
These statements are equivalent:
535
536
```Python
537
>>> if a is True:
538
>>> pass
539
>>> if a is not False:
540
>>> pass
541
>>> if a:
542
>>> pass
543
```
544
545
And these as well:
546
547
```Python
548
>>> if a is False:
549
>>> pass
550
>>> if a is not True:
551
>>> pass
552
>>> if not a:
553
>>> pass
554
```
555
556
[_Return to the Top_](#python-cheatsheet)
557
558
### Boolean Operators
559
560
There are three Boolean operators: and, or, and not.
561
562
The _and_ Operator’s _Truth_ Table:
563
564
| Expression | Evaluates to |
565
| ----------------- | ------------ |
566
| `True and True` | `True` |
567
| `True and False` | `False` |
568
| `False and True` | `False` |
569
| `False and False` | `False` |
570
571
The _or_ Operator’s _Truth_ Table:
572
573
| Expression | Evaluates to |
574
| ---------------- | ------------ |
575
| `True or True` | `True` |
576
| `True or False` | `True` |
577
| `False or True` | `True` |
578
| `False or False` | `False` |
579
580
The _not_ Operator’s _Truth_ Table:
581
582
| Expression | Evaluates to |
583
| ----------- | ------------ |
584
| `not True` | `False` |
585
| `not False` | `True` |
586
587
[_Return to the Top_](#python-cheatsheet)
588
589
### Mixing Boolean and Comparison Operators
590
591
```python
592
>>> (4 < 5) and (5 < 6)
593
True
594
```
595
596
```python
597
>>> (4 < 5) and (9 < 6)
598
False
599
```
600
601
```python
602
>>> (1 == 2) or (2 == 2)
603
True
604
```
605
606
You can also use multiple Boolean operators in an expression, along with the comparison operators:
607
608
```python
609
>>> 2 + 2 == 4 and not 2 + 2 == 5 and 2 * 2 == 2 + 2
610
True
611
```
612
613
[_Return to the Top_](#python-cheatsheet)
614
615
### if Statements
616
617
```python
618
if name == 'Alice':
619
print('Hi, Alice.')
620
```
621
622
[_Return to the Top_](#python-cheatsheet)
623
624
### else Statements
625
626
```python
627
name = 'Bob'
628
if name == 'Alice':
629
print('Hi, Alice.')
630
else:
631
print('Hello, stranger.')
632
```
633
634
[_Return to the Top_](#python-cheatsheet)
635
636
### elif Statements
637
638
```python
639
name = 'Bob'
640
age = 5
641
if name == 'Alice':
642
print('Hi, Alice.')
643
elif age < 12:
644
print('You are not Alice, kiddo.')
645
```
646
647
```python
648
name = 'Bob'
649
age = 30
650
if name == 'Alice':
651
print('Hi, Alice.')
652
elif age < 12:
653
print('You are not Alice, kiddo.')
654
else:
655
print('You are neither Alice nor a little kid.')
656
```
657
658
[_Return to the Top_](#python-cheatsheet)
659
660
### while Loop Statements
661
662
```python
663
spam = 0
664
while spam < 5:
665
print('Hello, world.')
666
spam = spam + 1
667
```
668
669
[_Return to the Top_](#python-cheatsheet)
670
671
### break Statements
672
673
If the execution reaches a break statement, it immediately exits the while loop’s clause:
674
675
```python
676
while True:
677
print('Please type your name.')
678
name = input()
679
if name == 'your name':
680
break
681
print('Thank you!')
682
```
683
684
[_Return to the Top_](#python-cheatsheet)
685
686
### continue Statements
687
688
When the program execution reaches a continue statement, the program execution immediately jumps back to the start of the loop.
689
690
```python
691
while True:
692
print('Who are you?')
693
name = input()
694
if name != 'Joe':
695
continue
696
print('Hello, Joe. What is the password? (It is a fish.)')
697
password = input()
698
if password == 'swordfish':
699
break
700
print('Access granted.')
701
```
702
703
[_Return to the Top_](#python-cheatsheet)
704
705
### for Loops and the range() Function
706
707
```python
708
>>> print('My name is')
709
>>> for i in range(5):
710
>>> print('Jimmy Five Times ({})'.format(str(i)))
711
My name is
712
Jimmy Five Times (0)
713
Jimmy Five Times (1)
714
Jimmy Five Times (2)
715
Jimmy Five Times (3)
716
Jimmy Five Times (4)
717
```
718
719
The _range()_ function can also be called with three arguments. The first two arguments will be the start and stop values, and the third will be the step argument. The step is the amount that the variable is increased by after each iteration.
720
721
```python
722
>>> for i in range(0, 10, 2):
723
>>> print(i)
724
0
725
2
726
4
727
6
728
8
729
```
730
731
You can even use a negative number for the step argument to make the for loop count down instead of up.
732
733
```python
734
>>> for i in range(5, -1, -1):
735
>>> print(i)
736
5
737
4
738
3
739
2
740
1
741
0
742
```
743
744
### For else statement
745
746
This allows to specify a statement to execute in case of the full loop has been executed. Only
747
useful when a `break` condition can occur in the loop:
748
749
```python
750
>>> for i in [1, 2, 3, 4, 5]:
751
>>> if i == 3:
752
>>> break
753
>>> else:
754
>>> print("only executed when no item of the list is equal to 3")
755
```
756
757
[_Return to the Top_](#python-cheatsheet)
758
759
### Importing Modules
760
761
```python
762
import random
763
for i in range(5):
764
print(random.randint(1, 10))
765
```
766
767
```python
768
import random, sys, os, math
769
```
770
771
```python
772
from random import *
773
```
774
775
[_Return to the Top_](#python-cheatsheet)
776
777
### Ending a Program Early with sys.exit()
778
779
```python
780
import sys
781
782
while True:
783
print('Type exit to exit.')
784
response = input()
785
if response == 'exit':
786
sys.exit()
787
print('You typed {}.'.format(response))
788
```
789
790
[_Return to the Top_](#python-cheatsheet)
791
792
## Functions
793
794
```python
795
>>> def hello(name):
796
>>> print('Hello {}'.format(name))
797
>>>
798
>>> hello('Alice')
799
>>> hello('Bob')
800
Hello Alice
801
Hello Bob
802
```
803
804
[_Return to the Top_](#python-cheatsheet)
805
806
### Return Values and return Statements
807
808
When creating a function using the def statement, you can specify what the return value should be with a return statement. A return statement consists of the following:
809
810
- The return keyword.
811
812
- The value or expression that the function should return.
813
814
```python
815
import random
816
def getAnswer(answerNumber):
817
if answerNumber == 1:
818
return 'It is certain'
819
elif answerNumber == 2:
820
return 'It is decidedly so'
821
elif answerNumber == 3:
822
return 'Yes'
823
elif answerNumber == 4:
824
return 'Reply hazy try again'
825
elif answerNumber == 5:
826
return 'Ask again later'
827
elif answerNumber == 6:
828
return 'Concentrate and ask again'
829
elif answerNumber == 7:
830
return 'My reply is no'
831
elif answerNumber == 8:
832
return 'Outlook not so good'
833
elif answerNumber == 9:
834
return 'Very doubtful'
835
836
r = random.randint(1, 9)
837
fortune = getAnswer(r)
838
print(fortune)
839
```
840
841
[_Return to the Top_](#python-cheatsheet)
842
843
### The None Value
844
845
```python
846
>>> spam = print('Hello!')
847
Hello!
848
```
849
850
```python
851
>>> spam is None
852
True
853
```
854
855
Note: never compare to `None` with the `==` operator. Always use `is`.
856
857
[_Return to the Top_](#python-cheatsheet)
858
859
### Keyword Arguments and print()
860
861
```python
862
>>> print('Hello', end='')
863
>>> print('World')
864
HelloWorld
865
```
866
867
```python
868
>>> print('cats', 'dogs', 'mice')
869
cats dogs mice
870
```
871
872
```python
873
>>> print('cats', 'dogs', 'mice', sep=',')
874
cats,dogs,mice
875
```
876
877
[_Return to the Top_](#python-cheatsheet)
878
879
### Local and Global Scope
880
881
- Code in the global scope cannot use any local variables.
882
883
- However, a local scope can access global variables.
884
885
- Code in a function’s local scope cannot use variables in any other local scope.
886
887
- You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam.
888
889
[_Return to the Top_](#python-cheatsheet)
890
891
### The global Statement
892
893
If you need to modify a global variable from within a function, use the global statement:
894
895
```python
896
>>> def spam():
897
>>> global eggs
898
>>> eggs = 'spam'
899
>>>
900
>>> eggs = 'global'
901
>>> spam()
902
>>> print(eggs)
903
spam
904
```
905
906
There are four rules to tell whether a variable is in a local scope or global scope:
907
908
1. If a variable is being used in the global scope (that is, outside of all functions), then it is always a global variable.
909
910
1. If there is a global statement for that variable in a function, it is a global variable.
911
912
1. Otherwise, if the variable is used in an assignment statement in the function, it is a local variable.
913
914
1. But if the variable is not used in an assignment statement, it is a global variable.
915
916
[_Return to the Top_](#python-cheatsheet)
917
918
## Exception Handling
919
920
### Basic exception handling
921
922
```python
923
>>> def spam(divideBy):
924
>>> try:
925
>>> return 42 / divideBy
926
>>> except ZeroDivisionError as e:
927
>>> print('Error: Invalid argument: {}'.format(e))
928
>>>
929
>>> print(spam(2))
930
>>> print(spam(12))
931
>>> print(spam(0))
932
>>> print(spam(1))
933
21.0
934
3.5
935
Error: Invalid argument: division by zero
936
None
937
42.0
938
```
939
940
[_Return to the Top_](#python-cheatsheet)
941
942
### Final code in exception handling
943
944
Code inside the `finally` section is always executed, no matter if an exception has been raised or
945
not, and even if an exception is not caught.
946
947
```python
948
>>> def spam(divideBy):
949
>>> try:
950
>>> return 42 / divideBy
951
>>> except ZeroDivisionError as e:
952
>>> print('Error: Invalid argument: {}'.format(e))
953
>>> finally:
954
>>> print("-- division finished --")
955
>>> print(spam(2))
956
-- division finished --
957
21.0
958
>>> print(spam(12))
959
-- division finished --
960
3.5
961
>>> print(spam(0))
962
Error: Invalid Argument division by zero
963
-- division finished --
964
None
965
>>> print(spam(1))
966
-- division finished --
967
42.0
968
```
969
970
[_Return to the Top_](#python-cheatsheet)
971
972
## Lists
973
974
```python
975
>>> spam = ['cat', 'bat', 'rat', 'elephant']
976
977
>>> spam
978
['cat', 'bat', 'rat', 'elephant']
979
```
980
981
[_Return to the Top_](#python-cheatsheet)
982
983
### Getting Individual Values in a List with Indexes
984
985
```python
986
>>> spam = ['cat', 'bat', 'rat', 'elephant']
987
>>> spam[0]
988
'cat'
989
```
990
991
```python
992
>>> spam[1]
993
'bat'
994
```
995
996
```python
997
>>> spam[2]
998
'rat'
999
```
1000
1001
```python
1002
>>> spam[3]
1003
'elephant'
1004
```
1005
1006
[_Return to the Top_](#python-cheatsheet)
1007
1008
### Negative Indexes
1009
1010
```python
1011
>>> spam = ['cat', 'bat', 'rat', 'elephant']
1012
>>> spam[-1]
1013
'elephant'
1014
```
1015
1016
```python
1017
>>> spam[-3]
1018
'bat'
1019
```
1020
1021
```python
1022
>>> 'The {} is afraid of the {}.'.format(spam[-1], spam[-3])
1023
'The elephant is afraid of the bat.'
1024
```
1025
1026
[_Return to the Top_](#python-cheatsheet)
1027
1028
### Getting Sublists with Slices
1029
1030
```python
1031
>>> spam = ['cat', 'bat', 'rat', 'elephant']
1032
>>> spam[0:4]
1033
['cat', 'bat', 'rat', 'elephant']
1034
```
1035
1036
```python
1037
>>> spam[1:3]
1038
['bat', 'rat']
1039
```
1040
1041
```python
1042
>>> spam[0:-1]
1043
['cat', 'bat', 'rat']
1044
```
1045
1046
```python
1047
>>> spam = ['cat', 'bat', 'rat', 'elephant']
1048
>>> spam[:2]
1049
['cat', 'bat']
1050
```
1051
1052
```python
1053
>>> spam[1:]
1054
['bat', 'rat', 'elephant']
1055
```
1056
1057
Slicing the complete list will perform a copy:
1058
1059
```python
1060
>>> spam2 = spam[:]
1061
['cat', 'bat', 'rat', 'elephant']
1062
>>> spam.append('dog')
1063
>>> spam
1064
['cat', 'bat', 'rat', 'elephant', 'dog']
1065
>>> spam2
1066
['cat', 'bat', 'rat', 'elephant']
1067
```
1068
1069
[_Return to the Top_](#python-cheatsheet)
1070
1071
### Getting a List’s Length with len()
1072
1073
```python
1074
>>> spam = ['cat', 'dog', 'moose']
1075
>>> len(spam)
1076
3
1077
```
1078
1079
[_Return to the Top_](#python-cheatsheet)
1080
1081
### Changing Values in a List with Indexes
1082
1083
```python
1084
>>> spam = ['cat', 'bat', 'rat', 'elephant']
1085
>>> spam[1] = 'aardvark'
1086
1087
>>> spam
1088
['cat', 'aardvark', 'rat', 'elephant']
1089
1090
>>> spam[2] = spam[1]
1091
1092
>>> spam
1093
['cat', 'aardvark', 'aardvark', 'elephant']
1094
1095
>>> spam[-1] = 12345
1096
1097
>>> spam
1098
['cat', 'aardvark', 'aardvark', 12345]
1099
```
1100
1101
[_Return to the Top_](#python-cheatsheet)
1102
1103
### List Concatenation and List Replication
1104
1105
```python
1106
>>> [1, 2, 3] + ['A', 'B', 'C']
1107
[1, 2, 3, 'A', 'B', 'C']
1108
1109
>>> ['X', 'Y', 'Z'] * 3
1110
['X', 'Y', 'Z', 'X', 'Y', 'Z', 'X', 'Y', 'Z']
1111
1112
>>> spam = [1, 2, 3]
1113
1114
>>> spam = spam + ['A', 'B', 'C']
1115
1116
>>> spam
1117
[1, 2, 3, 'A', 'B', 'C']
1118
```
1119
1120
[_Return to the Top_](#python-cheatsheet)
1121
1122
### Removing Values from Lists with del Statements
1123
1124
```python
1125
>>> spam = ['cat', 'bat', 'rat', 'elephant']
1126
>>> del spam[2]
1127
>>> spam
1128
['cat', 'bat', 'elephant']
1129
```
1130
1131
```python
1132
>>> del spam[2]
1133
>>> spam
1134
['cat', 'bat']
1135
```
1136
1137
[_Return to the Top_](#python-cheatsheet)
1138
1139
### Using for Loops with Lists
1140
1141
```python
1142
>>> supplies = ['pens', 'staplers', 'flame-throwers', 'binders']
1143
>>> for i, supply in enumerate(supplies):
1144
>>> print('Index {} in supplies is: {}'.format(str(i), supply))
1145
Index 0 in supplies is: pens
1146
Index 1 in supplies is: staplers
1147
Index 2 in supplies is: flame-throwers
1148
Index 3 in supplies is: binders
1149
```
1150
1151
[_Return to the Top_](#python-cheatsheet)
1152
1153
### Looping Through Multiple Lists with zip()
1154
1155
```python
1156
>>> name = ['Pete', 'John', 'Elizabeth']
1157
>>> age = [6, 23, 44]
1158
>>> for n, a in zip(name, age):
1159
>>> print('{} is {} years old'.format(n, a))
1160
Pete is 6 years old
1161
John is 23 years old
1162
Elizabeth is 44 years old
1163
```
1164
1165
### The in and not in Operators
1166
1167
```python
1168
>>> 'howdy' in ['hello', 'hi', 'howdy', 'heyas']
1169
True
1170
```
1171
1172
```python
1173
>>> spam = ['hello', 'hi', 'howdy', 'heyas']
1174
>>> 'cat' in spam
1175
False
1176
```
1177
1178
```python
1179
>>> 'howdy' not in spam
1180
False
1181
```
1182
1183
```python
1184
>>> 'cat' not in spam
1185
True
1186
```
1187
1188
[_Return to the Top_](#python-cheatsheet)
1189
1190
### The Multiple Assignment Trick
1191
1192
The multiple assignment trick is a shortcut that lets you assign multiple variables with the values in a list in one line of code. So instead of doing this:
1193
1194
```python
1195
>>> cat = ['fat', 'orange', 'loud']
1196
1197
>>> size = cat[0]
1198
1199
>>> color = cat[1]
1200
1201
>>> disposition = cat[2]
1202
```
1203
1204
You could type this line of code:
1205
1206
```python
1207
>>> cat = ['fat', 'orange', 'loud']
1208
1209
>>> size, color, disposition = cat
1210
```
1211
1212
The multiple assignment trick can also be used to swap the values in two variables:
1213
1214
```python
1215
>>> a, b = 'Alice', 'Bob'
1216
>>> a, b = b, a
1217
>>> print(a)
1218
'Bob'
1219
```
1220
1221
```python
1222
>>> print(b)
1223
'Alice'
1224
```
1225
1226
[_Return to the Top_](#python-cheatsheet)
1227
1228
### Augmented Assignment Operators
1229
1230
| Operator | Equivalent |
1231
| ----------- | ----------------- |
1232
| `spam += 1` | `spam = spam + 1` |
1233
| `spam -= 1` | `spam = spam - 1` |
1234
| `spam *= 1` | `spam = spam * 1` |
1235
| `spam /= 1` | `spam = spam / 1` |
1236
| `spam %= 1` | `spam = spam % 1` |
1237
1238
Examples:
1239
1240
```python
1241
>>> spam = 'Hello'
1242
>>> spam += ' world!'
1243
>>> spam
1244
'Hello world!'
1245
1246
>>> bacon = ['Zophie']
1247
>>> bacon *= 3
1248
>>> bacon
1249
['Zophie', 'Zophie', 'Zophie']
1250
```
1251
1252
[_Return to the Top_](#python-cheatsheet)
1253
1254
### Finding a Value in a List with the index() Method
1255
1256
```python
1257
>>> spam = ['Zophie', 'Pooka', 'Fat-tail', 'Pooka']
1258
1259
>>> spam.index('Pooka')
1260
1
1261
```
1262
1263
[_Return to the Top_](#python-cheatsheet)
1264
1265
### Adding Values to Lists with the append() and insert() Methods
1266
1267
**append()**:
1268
1269
```python
1270
>>> spam = ['cat', 'dog', 'bat']
1271
1272
>>> spam.append('moose')
1273
1274
>>> spam
1275
['cat', 'dog', 'bat', 'moose']
1276
```
1277
1278
**insert()**:
1279
1280
```python
1281
>>> spam = ['cat', 'dog', 'bat']
1282
1283
>>> spam.insert(1, 'chicken')
1284
1285
>>> spam
1286
['cat', 'chicken', 'dog', 'bat']
1287
```
1288
1289
[_Return to the Top_](#python-cheatsheet)
1290
1291
### Removing Values from Lists with remove()
1292
1293
```python
1294
>>> spam = ['cat', 'bat', 'rat', 'elephant']
1295
1296
>>> spam.remove('bat')
1297
1298
>>> spam
1299
['cat', 'rat', 'elephant']
1300
```
1301
1302
If the value appears multiple times in the list, only the first instance of the value will be removed.
1303
1304
[_Return to the Top_](#python-cheatsheet)
1305
1306
### Removing Values from Lists with pop()
1307
1308
```python
1309
>>> spam = ['cat', 'bat', 'rat', 'elephant']
1310
1311
>>> spam.pop()
1312
'elephant'
1313
1314
>>> spam
1315
['cat', 'bat', 'rat']
1316
1317
>>> spam.pop(0)
1318
'cat'
1319
1320
>>> spam
1321
['bat', 'rat']
1322
```
1323
1324
[_Return to the Top_](#python-cheatsheet)
1325
1326
### Sorting the Values in a List with the sort() Method
1327
1328
```python
1329
>>> spam = [2, 5, 3.14, 1, -7]
1330
>>> spam.sort()
1331
>>> spam
1332
[-7, 1, 2, 3.14, 5]
1333
```
1334
1335
```python
1336
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
1337
>>> spam.sort()
1338
>>> spam
1339
['ants', 'badgers', 'cats', 'dogs', 'elephants']
1340
```
1341
1342
You can also pass True for the reverse keyword argument to have sort() sort the values in reverse order:
1343
1344
```python
1345
>>> spam.sort(reverse=True)
1346
>>> spam
1347
['elephants', 'dogs', 'cats', 'badgers', 'ants']
1348
```
1349
1350
If you need to sort the values in regular alphabetical order, pass str. lower for the key keyword argument in the sort() method call:
1351
1352
```python
1353
>>> spam = ['a', 'z', 'A', 'Z']
1354
>>> spam.sort(key=str.lower)
1355
>>> spam
1356
['a', 'A', 'z', 'Z']
1357
```
1358
1359
You can use the built-in function `sorted` to return a new list:
1360
1361
```python
1362
>>> spam = ['ants', 'cats', 'dogs', 'badgers', 'elephants']
1363
>>> sorted(spam)
1364
['ants', 'badgers', 'cats', 'dogs', 'elephants']
1365
```
1366
1367
[_Return to the Top_](#python-cheatsheet)
1368
1369
### Tuple Data Type
1370
1371
```python
1372
>>> eggs = ('hello', 42, 0.5)
1373
>>> eggs[0]
1374
'hello'
1375
```
1376
1377
```python
1378
>>> eggs[1:3]
1379
(42, 0.5)
1380
```
1381
1382
```python
1383
>>> len(eggs)
1384
3
1385
```
1386
1387
The main way that tuples are different from lists is that tuples, like strings, are immutable.
1388
1389
[_Return to the Top_](#python-cheatsheet)
1390
1391
### Converting Types with the list() and tuple() Functions
1392
1393
```python
1394
>>> tuple(['cat', 'dog', 5])
1395
('cat', 'dog', 5)
1396
```
1397
1398
```python
1399
>>> list(('cat', 'dog', 5))
1400
['cat', 'dog', 5]
1401
```
1402
1403
```python
1404
>>> list('hello')
1405
['h', 'e', 'l', 'l', 'o']
1406
```
1407
1408
[_Return to the Top_](#python-cheatsheet)
1409
1410
## Dictionaries and Structuring Data
1411
1412
Example Dictionary:
1413
1414
```python
1415
myCat = {'size': 'fat', 'color': 'gray', 'disposition': 'loud'}
1416
```
1417
1418
[_Return to the Top_](#python-cheatsheet)
1419
1420
### The keys(), values(), and items() Methods
1421
1422
values():
1423
1424
```python
1425
>>> spam = {'color': 'red', 'age': 42}
1426
>>> for v in spam.values():
1427
>>> print(v)
1428
red
1429
42
1430
```
1431
1432
keys():
1433
1434
```python
1435
>>> for k in spam.keys():
1436
>>> print(k)
1437
color
1438
age
1439
```
1440
1441
items():
1442
1443
```python
1444
>>> for i in spam.items():
1445
>>> print(i)
1446
('color', 'red')
1447
('age', 42)
1448
```
1449
1450
Using the keys(), values(), and items() methods, a for loop can iterate over the keys, values, or key-value pairs in a dictionary, respectively.
1451
1452
```python
1453
1454
>>> spam = {'color': 'red', 'age': 42}
1455
>>>
1456
>>> for k, v in spam.items():
1457
>>> print('Key: {} Value: {}'.format(k, str(v)))
1458
Key: age Value: 42
1459
Key: color Value: red
1460
```
1461
1462
[_Return to the Top_](#python-cheatsheet)
1463
1464
### Checking Whether a Key or Value Exists in a Dictionary
1465
1466
```python
1467
>>> spam = {'name': 'Zophie', 'age': 7}
1468
```
1469
1470
```python
1471
>>> 'name' in spam.keys()
1472
True
1473
```
1474
1475
```python
1476
>>> 'Zophie' in spam.values()
1477
True
1478
```
1479
1480
```python
1481
>>> # You can omit the call to keys() when checking for a key
1482
>>> 'color' in spam
1483
False
1484
```
1485
1486
```python
1487
>>> 'color' not in spam
1488
True
1489
```
1490
1491
[_Return to the Top_](#python-cheatsheet)
1492
1493
### The get() Method
1494
1495
Get has two parameters: key and default value if the key did not exist
1496
1497
```python
1498
>>> picnic_items = {'apples': 5, 'cups': 2}
1499
1500
>>> 'I am bringing {} cups.'.format(str(picnic_items.get('cups', 0)))
1501
'I am bringing 2 cups.'
1502
```
1503
1504
```python
1505
>>> 'I am bringing {} eggs.'.format(str(picnic_items.get('eggs', 0)))
1506
'I am bringing 0 eggs.'
1507
```
1508
1509
[_Return to the Top_](#python-cheatsheet)
1510
1511
### The setdefault() Method
1512
1513
Let's consider this code:
1514
1515
```python
1516
spam = {'name': 'Pooka', 'age': 5}
1517
1518
if 'color' not in spam:
1519
spam['color'] = 'black'
1520
```
1521
1522
Using `setdefault` we could write the same code more succinctly:
1523
1524
```python
1525
>>> spam = {'name': 'Pooka', 'age': 5}
1526
>>> spam.setdefault('color', 'black')
1527
'black'
1528
```
1529
1530
```python
1531
>>> spam
1532
{'color': 'black', 'age': 5, 'name': 'Pooka'}
1533
```
1534
1535
```python
1536
>>> spam.setdefault('color', 'white')
1537
'black'
1538
```
1539
1540
```python
1541
>>> spam
1542
{'color': 'black', 'age': 5, 'name': 'Pooka'}
1543
```
1544
1545
[_Return to the Top_](#python-cheatsheet)
1546
1547
### Pretty Printing
1548
1549
```python
1550
>>> import pprint
1551
>>>
1552
>>> message = 'It was a bright cold day in April, and the clocks were striking
1553
>>> thirteen.'
1554
>>> count = {}
1555
>>>
1556
>>> for character in message:
1557
>>> count.setdefault(character, 0)
1558
>>> count[character] = count[character] + 1
1559
>>>
1560
>>> pprint.pprint(count)
1561
{' ': 13,
1562
',': 1,
1563
'.': 1,
1564
'A': 1,
1565
'I': 1,
1566
'a': 4,
1567
'b': 1,
1568
'c': 3,
1569
'd': 3,
1570
'e': 5,
1571
'g': 2,
1572
'h': 3,
1573
'i': 6,
1574
'k': 2,
1575
'l': 3,
1576
'n': 4,
1577
'o': 2,
1578
'p': 1,
1579
'r': 5,
1580
's': 3,
1581
't': 6,
1582
'w': 2,
1583
'y': 1}
1584
```
1585
1586
[_Return to the Top_](#python-cheatsheet)
1587
1588
### Merge two dictionaries
1589
1590
```python
1591
# in Python 3.5+:
1592
>>> x = {'a': 1, 'b': 2}
1593
>>> y = {'b': 3, 'c': 4}
1594
>>> z = {**x, **y}
1595
>>> z
1596
{'c': 4, 'a': 1, 'b': 3}
1597
1598
# in Python 2.7
1599
>>> z = dict(x, **y)
1600
>>> z
1601
{'c': 4, 'a': 1, 'b': 3}
1602
```
1603
1604
## sets
1605
1606
From the Python 3 [documentation](https://docs.python.org/3/tutorial/datastructures.html)
1607
1608
> A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference.
1609
1610
### Initializing a set
1611
1612
There are two ways to create sets: using curly braces `{}` and the built-in function `set()`
1613
1614
```python
1615
>>> s = {1, 2, 3}
1616
>>> s = set([1, 2, 3])
1617
```
1618
1619
When creating an empty set, be sure to not use the curly braces `{}` or you will get an empty dictionary instead.
1620
1621
```python
1622
>>> s = {}
1623
>>> type(s)
1624
<class 'dict'>
1625
```
1626
1627
### sets: unordered collections of unique elements
1628
1629
A set automatically remove all the duplicate values.
1630
1631
```python
1632
>>> s = {1, 2, 3, 2, 3, 4}
1633
>>> s
1634
{1, 2, 3, 4}
1635
```
1636
1637
And as an unordered data type, they can't be indexed.
1638
1639
```python
1640
>>> s = {1, 2, 3}
1641
>>> s[0]
1642
Traceback (most recent call last):
1643
File "<stdin>", line 1, in <module>
1644
TypeError: 'set' object does not support indexing
1645
>>>
1646
```
1647
1648
### set add() and update()
1649
1650
Using the `add()` method we can add a single element to the set.
1651
1652
```python
1653
>>> s = {1, 2, 3}
1654
>>> s.add(4)
1655
>>> s
1656
{1, 2, 3, 4}
1657
```
1658
1659
And with `update()`, multiple ones .
1660
1661
```python
1662
>>> s = {1, 2, 3}
1663
>>> s.update([2, 3, 4, 5, 6])
1664
>>> s
1665
{1, 2, 3, 4, 5, 6} # remember, sets automatically remove duplicates
1666
```
1667
1668
### set remove() and discard()
1669
1670
Both methods will remove an element from the set, but `remove()` will raise a `key error` if the value doesn't exist.
1671
1672
```python
1673
>>> s = {1, 2, 3}
1674
>>> s.remove(3)
1675
>>> s
1676
{1, 2}
1677
>>> s.remove(3)
1678
Traceback (most recent call last):
1679
File "<stdin>", line 1, in <module>
1680
KeyError: 3
1681
```
1682
1683
`discard()` won't raise any errors.
1684
1685
```python
1686
>>> s = {1, 2, 3}
1687
>>> s.discard(3)
1688
>>> s
1689
{1, 2}
1690
>>> s.discard(3)
1691
>>>
1692
```
1693
1694
### set union()
1695
1696
`union()` or `|` will create a new set that contains all the elements from the sets provided.
1697
1698
```python
1699
>>> s1 = {1, 2, 3}
1700
>>> s2 = {3, 4, 5}
1701
>>> s1.union(s2) # or 's1 | s2'
1702
{1, 2, 3, 4, 5}
1703
```
1704
1705
### set intersection
1706
1707
`intersection` or `&` will return a set containing only the elements that are common to all of them.
1708
1709
```python
1710
>>> s1 = {1, 2, 3}
1711
>>> s2 = {2, 3, 4}
1712
>>> s3 = {3, 4, 5}
1713
>>> s1.intersection(s2, s3) # or 's1 & s2 & s3'
1714
{3}
1715
```
1716
1717
### set difference
1718
1719
`difference` or `-` will return only the elements that are unique to the first set (invoked set).
1720
1721
```python
1722
>>> s1 = {1, 2, 3}
1723
>>> s2 = {2, 3, 4}
1724
>>> s1.difference(s2) # or 's1 - s2'
1725
{1}
1726
>>> s2.difference(s1) # or 's2 - s1'
1727
{4}
1728
```
1729
1730
### set symetric_difference
1731
1732
`symetric_difference` or `^` will return all the elements that are not common between them.
1733
1734
```python
1735
>>> s1 = {1, 2, 3}
1736
>>> s2 = {2, 3, 4}
1737
>>> s1.symmetric_difference(s2) # or 's1 ^ s2'
1738
{1, 4}
1739
```
1740
1741
[_Return to the Top_](#python-cheatsheet)
1742
1743
## itertools Module
1744
1745
The _itertools_ module is a collection of tools intended to be fast and use memory efficiently when handling iterators (like [lists](#lists) or [dictionaries](#dictionaries-and-structuring-data)).
1746
1747
From the official [Python 3.x documentation](https://docs.python.org/3/library/itertools.html):
1748
1749
> The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Together, they form an “iterator algebra” making it possible to construct specialized tools succinctly and efficiently in pure Python.
1750
1751
The _itertools_ module comes in the standard library and must be imported.
1752
1753
The [operator](https://docs.python.org/3/library/operator.html) module will also be used. This module is not necessary when using itertools, but needed for some of the examples below.
1754
1755
[_Return to the Top_](#python-cheatsheet)
1756
1757
### accumulate()
1758
1759
Makes an iterator that returns the results of a function.
1760
1761
```python
1762
itertools.accumulate(iterable[, func])
1763
```
1764
1765
Example:
1766
1767
```python
1768
>>> data = [1, 2, 3, 4, 5]
1769
>>> result = itertools.accumulate(data, operator.mul)
1770
>>> for each in result:
1771
>>> print(each)
1772
1
1773
2
1774
6
1775
24
1776
120
1777
```
1778
1779
The operator.mul takes two numbers and multiplies them:
1780
1781
```python
1782
operator.mul(1, 2)
1783
2
1784
operator.mul(2, 3)
1785
6
1786
operator.mul(6, 4)
1787
24
1788
operator.mul(24, 5)
1789
120
1790
```
1791
1792
Passing a function is optional:
1793
1794
```python
1795
>>> data = [5, 2, 6, 4, 5, 9, 1]
1796
>>> result = itertools.accumulate(data)
1797
>>> for each in result:
1798
>>> print(each)
1799
5
1800
7
1801
13
1802
17
1803
22
1804
31
1805
32
1806
```
1807
1808
If no function is designated the items will be summed:
1809
1810
```python
1811
5
1812
5 + 2 = 7
1813
7 + 6 = 13
1814
13 + 4 = 17
1815
17 + 5 = 22
1816
22 + 9 = 31
1817
31 + 1 = 32
1818
```
1819
1820
[_Return to the Top_](#python-cheatsheet)
1821
1822
### combinations()
1823
1824
Takes an iterable and a integer. This will create all the unique combination that have r members.
1825
1826
```python
1827
itertools.combinations(iterable, r)
1828
```
1829
1830
Example:
1831
1832
```python
1833
>>> shapes = ['circle', 'triangle', 'square',]
1834
>>> result = itertools.combinations(shapes, 2)
1835
>>> for each in result:
1836
>>> print(each)
1837
('circle', 'triangle')
1838
('circle', 'square')
1839
('triangle', 'square')
1840
```
1841
1842
[_Return to the Top_](#python-cheatsheet)
1843
1844
### combinations_with_replacement()
1845
1846
Just like combinations(), but allows individual elements to be repeated more than once.
1847
1848
```python
1849
itertools.combinations_with_replacement(iterable, r)
1850
```
1851
1852
Example:
1853
1854
```python
1855
>>> shapes = ['circle', 'triangle', 'square']
1856
>>> result = itertools.combinations_with_replacement(shapes, 2)
1857
>>> for each in result:
1858
>>> print(each)
1859
('circle', 'circle')
1860
('circle', 'triangle')
1861
('circle', 'square')
1862
('triangle', 'triangle')
1863
('triangle', 'square')
1864
('square', 'square')
1865
```
1866
1867
[_Return to the Top_](#python-cheatsheet)
1868
1869
### count()
1870
1871
Makes an iterator that returns evenly spaced values starting with number start.
1872
1873
```python
1874
itertools.count(start=0, step=1)
1875
```
1876
1877
Example:
1878
1879
```python
1880
>>> for i in itertools.count(10,3):
1881
>>> print(i)
1882
>>> if i > 20:
1883
>>> break
1884
10
1885
13
1886
16
1887
19
1888
22
1889
```
1890
1891
[_Return to the Top_](#python-cheatsheet)
1892
1893
### cycle()
1894
1895
This function cycles through an iterator endlessly.
1896
1897
```python
1898
itertools.cycle(iterable)
1899
```
1900
1901
Example:
1902
1903
```python
1904
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue', 'violet']
1905
>>> for color in itertools.cycle(colors):
1906
>>> print(color)
1907
red
1908
orange
1909
yellow
1910
green
1911
blue
1912
violet
1913
red
1914
orange
1915
```
1916
1917
When reached the end of the iterable it start over again from the beginning.
1918
1919
[_Return to the Top_](#python-cheatsheet)
1920
1921
### chain()
1922
1923
Take a series of iterables and return them as one long iterable.
1924
1925
```python
1926
itertools.chain(*iterables)
1927
```
1928
1929
Example:
1930
1931
```python
1932
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
1933
>>> shapes = ['circle', 'triangle', 'square', 'pentagon']
1934
>>> result = itertools.chain(colors, shapes)
1935
>>> for each in result:
1936
>>> print(each)
1937
red
1938
orange
1939
yellow
1940
green
1941
blue
1942
circle
1943
triangle
1944
square
1945
pentagon
1946
```
1947
1948
[_Return to the Top_](#python-cheatsheet)
1949
1950
### compress()
1951
1952
Filters one iterable with another.
1953
1954
```python
1955
itertools.compress(data, selectors)
1956
```
1957
1958
Example:
1959
1960
```python
1961
>>> shapes = ['circle', 'triangle', 'square', 'pentagon']
1962
>>> selections = [True, False, True, False]
1963
>>> result = itertools.compress(shapes, selections)
1964
>>> for each in result:
1965
>>> print(each)
1966
circle
1967
square
1968
```
1969
1970
[_Return to the Top_](#python-cheatsheet)
1971
1972
### dropwhile()
1973
1974
Make an iterator that drops elements from the iterable as long as the predicate is true; afterwards, returns every element.
1975
1976
```python
1977
itertools.dropwhile(predicate, iterable)
1978
```
1979
1980
Example:
1981
1982
```python
1983
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
1984
>>> result = itertools.dropwhile(lambda x: x<5, data)
1985
>>> for each in result:
1986
>>> print(each)
1987
5
1988
6
1989
7
1990
8
1991
9
1992
10
1993
1
1994
```
1995
1996
[_Return to the Top_](#python-cheatsheet)
1997
1998
### filterfalse()
1999
2000
Makes an iterator that filters elements from iterable returning only those for which the predicate is False.
2001
2002
```python
2003
itertools.filterfalse(predicate, iterable)
2004
```
2005
2006
Example:
2007
2008
```python
2009
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
2010
>>> result = itertools.filterfalse(lambda x: x<5, data)
2011
>>> for each in result:
2012
>>> print(each)
2013
5
2014
6
2015
7
2016
8
2017
9
2018
10
2019
```
2020
2021
[_Return to the Top_](#python-cheatsheet)
2022
2023
### groupby()
2024
2025
Simply put, this function groups things together.
2026
2027
```python
2028
itertools.groupby(iterable, key=None)
2029
```
2030
2031
Example:
2032
2033
```python
2034
>>> robots = [{
2035
'name': 'blaster',
2036
'faction': 'autobot'
2037
}, {
2038
'name': 'galvatron',
2039
'faction': 'decepticon'
2040
}, {
2041
'name': 'jazz',
2042
'faction': 'autobot'
2043
}, {
2044
'name': 'metroplex',
2045
'faction': 'autobot'
2046
}, {
2047
'name': 'megatron',
2048
'faction': 'decepticon'
2049
}, {
2050
'name': 'starcream',
2051
'faction': 'decepticon'
2052
}]
2053
>>> for key, group in itertools.groupby(robots, key=lambda x: x['faction']):
2054
>>> print(key)
2055
>>> print(list(group))
2056
autobot
2057
[{'name': 'blaster', 'faction': 'autobot'}]
2058
decepticon
2059
[{'name': 'galvatron', 'faction': 'decepticon'}]
2060
autobot
2061
[{'name': 'jazz', 'faction': 'autobot'}, {'name': 'metroplex', 'faction': 'autobot'}]
2062
decepticon
2063
[{'name': 'megatron', 'faction': 'decepticon'}, {'name': 'starcream', 'faction': 'decepticon'}]
2064
```
2065
2066
[_Return to the Top_](#python-cheatsheet)
2067
2068
### islice()
2069
2070
This function is very much like slices. This allows you to cut out a piece of an iterable.
2071
2072
```python
2073
itertools.islice(iterable, start, stop[, step])
2074
```
2075
2076
Example:
2077
2078
```python
2079
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue',]
2080
>>> few_colors = itertools.islice(colors, 2)
2081
>>> for each in few_colors:
2082
>>> print(each)
2083
red
2084
orange
2085
```
2086
2087
[_Return to the Top_](#python-cheatsheet)
2088
2089
### permutations()
2090
2091
```python
2092
itertools.permutations(iterable, r=None)
2093
```
2094
2095
Example:
2096
2097
```python
2098
>>> alpha_data = ['a', 'b', 'c']
2099
>>> result = itertools.permutations(alpha_data)
2100
>>> for each in result:
2101
>>> print(each)
2102
('a', 'b', 'c')
2103
('a', 'c', 'b')
2104
('b', 'a', 'c')
2105
('b', 'c', 'a')
2106
('c', 'a', 'b')
2107
('c', 'b', 'a')
2108
```
2109
2110
[_Return to the Top_](#python-cheatsheet)
2111
2112
### product()
2113
2114
Creates the cartesian products from a series of iterables.
2115
2116
```python
2117
>>> num_data = [1, 2, 3]
2118
>>> alpha_data = ['a', 'b', 'c']
2119
>>> result = itertools.product(num_data, alpha_data)
2120
>>> for each in result:
2121
print(each)
2122
(1, 'a')
2123
(1, 'b')
2124
(1, 'c')
2125
(2, 'a')
2126
(2, 'b')
2127
(2, 'c')
2128
(3, 'a')
2129
(3, 'b')
2130
(3, 'c')
2131
```
2132
2133
[_Return to the Top_](#python-cheatsheet)
2134
2135
### repeat()
2136
2137
This function will repeat an object over and over again. Unless, there is a times argument.
2138
2139
```python
2140
itertools.repeat(object[, times])
2141
```
2142
2143
Example:
2144
2145
```python
2146
>>> for i in itertools.repeat("spam", 3):
2147
print(i)
2148
spam
2149
spam
2150
spam
2151
```
2152
2153
[_Return to the Top_](#python-cheatsheet)
2154
2155
### starmap()
2156
2157
Makes an iterator that computes the function using arguments obtained from the iterable.
2158
2159
```python
2160
itertools.starmap(function, iterable)
2161
```
2162
2163
Example:
2164
2165
```python
2166
>>> data = [(2, 6), (8, 4), (7, 3)]
2167
>>> result = itertools.starmap(operator.mul, data)
2168
>>> for each in result:
2169
>>> print(each)
2170
12
2171
32
2172
21
2173
```
2174
2175
[_Return to the Top_](#python-cheatsheet)
2176
2177
### takewhile()
2178
2179
The opposite of dropwhile(). Makes an iterator and returns elements from the iterable as long as the predicate is true.
2180
2181
```python
2182
itertools.takewhile(predicate, iterable)
2183
```
2184
2185
Example:
2186
2187
```python
2188
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1]
2189
>>> result = itertools.takewhile(lambda x: x<5, data)
2190
>>> for each in result:
2191
>>> print(each)
2192
1
2193
2
2194
3
2195
4
2196
```
2197
2198
[_Return to the Top_](#python-cheatsheet)
2199
2200
### tee()
2201
2202
Return n independent iterators from a single iterable.
2203
2204
```python
2205
itertools.tee(iterable, n=2)
2206
```
2207
2208
Example:
2209
2210
```python
2211
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
2212
>>> alpha_colors, beta_colors = itertools.tee(colors)
2213
>>> for each in alpha_colors:
2214
>>> print(each)
2215
red
2216
orange
2217
yellow
2218
green
2219
blue
2220
```
2221
2222
```python
2223
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue']
2224
>>> alpha_colors, beta_colors = itertools.tee(colors)
2225
>>> for each in beta_colors:
2226
>>> print(each)
2227
red
2228
orange
2229
yellow
2230
green
2231
blue
2232
```
2233
2234
[_Return to the Top_](#python-cheatsheet)
2235
2236
### zip_longest()
2237
2238
Makes an iterator that aggregates elements from each of the iterables. If the iterables are of uneven length, missing values are filled-in with fillvalue. Iteration continues until the longest iterable is exhausted.
2239
2240
```python
2241
itertools.zip_longest(*iterables, fillvalue=None)
2242
```
2243
2244
Example:
2245
2246
```python
2247
>>> colors = ['red', 'orange', 'yellow', 'green', 'blue',]
2248
>>> data = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10,]
2249
>>> for each in itertools.zip_longest(colors, data, fillvalue=None):
2250
>>> print(each)
2251
('red', 1)
2252
('orange', 2)
2253
('yellow', 3)
2254
('green', 4)
2255
('blue', 5)
2256
(None, 6)
2257
(None, 7)
2258
(None, 8)
2259
(None, 9)
2260
(None, 10)
2261
```
2262
2263
[_Return to the Top_](#python-cheatsheet)
2264
2265
## Comprehensions
2266
2267
### List comprehension
2268
2269
```python
2270
>>> a = [1, 3, 5, 7, 9, 11]
2271
2272
>>> [i - 1 for i in a]
2273
[0, 2, 4, 6, 8, 10]
2274
```
2275
2276
### Set comprehension
2277
2278
```python
2279
>>> b = {"abc", "def"}
2280
>>> {s.upper() for s in b}
2281
{"ABC", "DEF"}
2282
```
2283
2284
### Dict comprehension
2285
2286
```python
2287
>>> c = {'name': 'Pooka', 'age': 5}
2288
>>> {v: k for k, v in c.items()}
2289
{'Pooka': 'name', 5: 'age'}
2290
```
2291
2292
A List comprehension can be generated from a dictionary:
2293
2294
```python
2295
>>> c = {'name': 'Pooka', 'first_name': 'Oooka'}
2296
>>> ["{}:{}".format(k.upper(), v.upper()) for k, v in c.items()]
2297
['NAME:POOKA', 'FIRST_NAME:OOOKA']
2298
```
2299
2300
## Manipulating Strings
2301
2302
### Escape Characters
2303
2304
| Escape character | Prints as |
2305
| ---------------- | -------------------- |
2306
| `\'` | Single quote |
2307
| `\"` | Double quote |
2308
| `\t` | Tab |
2309
| `\n` | Newline (line break) |
2310
| `\\` | Backslash |
2311
2312
Example:
2313
2314
```python
2315
>>> print("Hello there!\nHow are you?\nI\'m doing fine.")
2316
Hello there!
2317
How are you?
2318
I'm doing fine.
2319
```
2320
2321
[_Return to the Top_](#python-cheatsheet)
2322
2323
### Raw Strings
2324
2325
A raw string completely ignores all escape characters and prints any backslash that appears in the string.
2326
2327
```python
2328
>>> print(r'That is Carol\'s cat.')
2329
That is Carol\'s cat.
2330
```
2331
2332
Note: mostly used for regular expression definition (see `re` package)
2333
2334
[_Return to the Top_](#python-cheatsheet)
2335
2336
### Multiline Strings with Triple Quotes
2337
2338
```python
2339
>>> print('''Dear Alice,
2340
>>>
2341
>>> Eve's cat has been arrested for catnapping, cat burglary, and extortion.
2342
>>>
2343
>>> Sincerely,
2344
>>> Bob''')
2345
Dear Alice,
2346
2347
Eve's cat has been arrested for catnapping, cat burglary, and extortion.
2348
2349
Sincerely,
2350
Bob
2351
```
2352
2353
To keep a nicer flow in your code, you can use the `dedent` function from the `textwrap` standard package.
2354
2355
```python
2356
>>> from textwrap import dedent
2357
>>>
2358
>>> def my_function():
2359
>>> print('''
2360
>>> Dear Alice,
2361
>>>
2362
>>> Eve's cat has been arrested for catnapping, cat burglary, and extortion.
2363
>>>
2364
>>> Sincerely,
2365
>>> Bob
2366
>>> ''').strip()
2367
```
2368
2369
This generates the same string than before.
2370
2371
[_Return to the Top_](#python-cheatsheet)
2372
2373
### Indexing and Slicing Strings
2374
2375
H e l l o w o r l d !
2376
0 1 2 3 4 5 6 7 8 9 10 11
2377
2378
```python
2379
>>> spam = 'Hello world!'
2380
2381
>>> spam[0]
2382
'H'
2383
```
2384
2385
```python
2386
>>> spam[4]
2387
'o'
2388
```
2389
2390
```python
2391
>>> spam[-1]
2392
'!'
2393
```
2394
2395
Slicing:
2396
2397
```python
2398
2399
>>> spam[0:5]
2400
'Hello'
2401
```
2402
2403
```python
2404
>>> spam[:5]
2405
'Hello'
2406
```
2407
2408
```python
2409
>>> spam[6:]
2410
'world!'
2411
```
2412
2413
```python
2414
>>> spam[6:-1]
2415
'world'
2416
```
2417
2418
```python
2419
>>> spam[:-1]
2420
'Hello world'
2421
```
2422
2423
```python
2424
>>> spam[::-1]
2425
'!dlrow olleH'
2426
```
2427
2428
```python
2429
>>> spam = 'Hello world!'
2430
>>> fizz = spam[0:5]
2431
>>> fizz
2432
'Hello'
2433
```
2434
2435
[_Return to the Top_](#python-cheatsheet)
2436
2437
### The in and not in Operators with Strings
2438
2439
```python
2440
>>> 'Hello' in 'Hello World'
2441
True
2442
```
2443
2444
```python
2445
>>> 'Hello' in 'Hello'
2446
True
2447
```
2448
2449
```python
2450
>>> 'HELLO' in 'Hello World'
2451
False
2452
```
2453
2454
```python
2455
>>> '' in 'spam'
2456
True
2457
```
2458
2459
```python
2460
>>> 'cats' not in 'cats and dogs'
2461
False
2462
```
2463
2464
### The in and not in Operators with list
2465
2466
```python
2467
>>> a = [1, 2, 3, 4]
2468
>>> 5 in a
2469
False
2470
```
2471
2472
```python
2473
>>> 2 in a
2474
True
2475
```
2476
2477
[_Return to the Top_](#python-cheatsheet)
2478
2479
### The upper(), lower(), isupper(), and islower() String Methods
2480
2481
`upper()` and `lower()`:
2482
2483
```python
2484
>>> spam = 'Hello world!'
2485
>>> spam = spam.upper()
2486
>>> spam
2487
'HELLO WORLD!'
2488
```
2489
2490
```python
2491
>>> spam = spam.lower()
2492
>>> spam
2493
'hello world!'
2494
```
2495
2496
isupper() and islower():
2497
2498
```python
2499
>>> spam = 'Hello world!'
2500
>>> spam.islower()
2501
False
2502
```
2503
2504
```python
2505
>>> spam.isupper()
2506
False
2507
```
2508
2509
```python
2510
>>> 'HELLO'.isupper()
2511
True
2512
```
2513
2514
```python
2515
>>> 'abc12345'.islower()
2516
True
2517
```
2518
2519
```python
2520
>>> '12345'.islower()
2521
False
2522
```
2523
2524
```python
2525
>>> '12345'.isupper()
2526
False
2527
```
2528
2529
[_Return to the Top_](#python-cheatsheet)
2530
2531
### The isX String Methods
2532
2533
- **isalpha()** returns True if the string consists only of letters and is not blank.
2534
- **isalnum()** returns True if the string consists only of letters and numbers and is not blank.
2535
- **isdecimal()** returns True if the string consists only of numeric characters and is not blank.
2536
- **isspace()** returns True if the string consists only of spaces,tabs, and new-lines and is not blank.
2537
- **istitle()** returns True if the string consists only of words that begin with an uppercase letter followed by only lowercase letters.
2538
2539
[_Return to the Top_](#python-cheatsheet)
2540
2541
### The startswith() and endswith() String Methods
2542
2543
```python
2544
>>> 'Hello world!'.startswith('Hello')
2545
True
2546
```
2547
2548
```python
2549
>>> 'Hello world!'.endswith('world!')
2550
True
2551
```
2552
2553
```python
2554
>>> 'abc123'.startswith('abcdef')
2555
False
2556
```
2557
2558
```python
2559
>>> 'abc123'.endswith('12')
2560
False
2561
```
2562
2563
```python
2564
>>> 'Hello world!'.startswith('Hello world!')
2565
True
2566
```
2567
2568
```python
2569
>>> 'Hello world!'.endswith('Hello world!')
2570
True
2571
```
2572
2573
[_Return to the Top_](#python-cheatsheet)
2574
2575
### The join() and split() String Methods
2576
2577
join():
2578
2579
```python
2580
>>> ', '.join(['cats', 'rats', 'bats'])
2581
'cats, rats, bats'
2582
```
2583
2584
```python
2585
>>> ' '.join(['My', 'name', 'is', 'Simon'])
2586
'My name is Simon'
2587
```
2588
2589
```python
2590
>>> 'ABC'.join(['My', 'name', 'is', 'Simon'])
2591
'MyABCnameABCisABCSimon'
2592
```
2593
2594
split():
2595
2596
```python
2597
>>> 'My name is Simon'.split()
2598
['My', 'name', 'is', 'Simon']
2599
```
2600
2601
```python
2602
>>> 'MyABCnameABCisABCSimon'.split('ABC')
2603
['My', 'name', 'is', 'Simon']
2604
```
2605
2606
```python
2607
>>> 'My name is Simon'.split('m')
2608
['My na', 'e is Si', 'on']
2609
```
2610
2611
[_Return to the Top_](#python-cheatsheet)
2612
2613
### Justifying Text with rjust(), ljust(), and center()
2614
2615
rjust() and ljust():
2616
2617
```python
2618
>>> 'Hello'.rjust(10)
2619
' Hello'
2620
```
2621
2622
```python
2623
>>> 'Hello'.rjust(20)
2624
' Hello'
2625
```
2626
2627
```python
2628
>>> 'Hello World'.rjust(20)
2629
' Hello World'
2630
```
2631
2632
```python
2633
>>> 'Hello'.ljust(10)
2634
'Hello '
2635
```
2636
2637
An optional second argument to rjust() and ljust() will specify a fill character other than a space character. Enter the following into the interactive shell:
2638
2639
```python
2640
>>> 'Hello'.rjust(20, '*')
2641
'***************Hello'
2642
```
2643
2644
```python
2645
>>> 'Hello'.ljust(20, '-')
2646
'Hello---------------'
2647
```
2648
2649
center():
2650
2651
```python
2652
>>> 'Hello'.center(20)
2653
' Hello '
2654
```
2655
2656
```python
2657
>>> 'Hello'.center(20, '=')
2658
'=======Hello========'
2659
```
2660
2661
[_Return to the Top_](#python-cheatsheet)
2662
2663
### Removing Whitespace with strip(), rstrip(), and lstrip()
2664
2665
```python
2666
>>> spam = ' Hello World '
2667
>>> spam.strip()
2668
'Hello World'
2669
```
2670
2671
```python
2672
>>> spam.lstrip()
2673
'Hello World '
2674
```
2675
2676
```python
2677
>>> spam.rstrip()
2678
' Hello World'
2679
```
2680
2681
```python
2682
>>> spam = 'SpamSpamBaconSpamEggsSpamSpam'
2683
>>> spam.strip('ampS')
2684
'BaconSpamEggs'
2685
```
2686
2687
[_Return to the Top_](#python-cheatsheet)
2688
2689
### Copying and Pasting Strings with the pyperclip Module (need pip install)
2690
2691
```python
2692
>>> import pyperclip
2693
2694
>>> pyperclip.copy('Hello world!')
2695
2696
>>> pyperclip.paste()
2697
'Hello world!'
2698
```
2699
2700
[_Return to the Top_](#python-cheatsheet)
2701
2702
## String Formatting
2703
2704
### % operator
2705
2706
```python
2707
>>> name = 'Pete'
2708
>>> 'Hello %s' % name
2709
"Hello Pete"
2710
```
2711
2712
We can use the `%x` format specifier to convert an int value to a string:
2713
2714
```python
2715
>>> num = 5
2716
>>> 'I have %x apples' % num
2717
"I have 5 apples"
2718
```
2719
2720
Note: For new code, using [str.format](#string-formatting-strformat) or [f-strings](#formatted-string-literals-or-f-strings-python-36) (Python 3.6+) is strongly recommended over the `%` operator.
2721
2722
[_Return to the Top_](#python-cheatsheet)
2723
2724
### String Formatting (str.format)
2725
2726
Python 3 introduced a new way to do string formatting that was later back-ported to Python 2.7. This makes the syntax for string formatting more regular.
2727
2728
```python
2729
>>> name = 'John'
2730
>>> age = 20'
2731
2732
>>> "Hello I'm {}, my age is {}".format(name, age)
2733
"Hello I'm John, my age is 20"
2734
```
2735
2736
```python
2737
>>> "Hello I'm {0}, my age is {1}".format(name, age)
2738
"Hello I'm John, my age is 20"
2739
```
2740
2741
The official [Python 3.x documentation](https://docs.python.org/3/library/stdtypes.html?highlight=sprintf#printf-style-string-formatting) recommend `str.format` over the `%` operator:
2742
2743
> The formatting operations described here exhibit a variety of quirks that lead to a number of common errors (such as failing to display tuples and dictionaries correctly). Using the newer formatted string literals or the str.format() interface helps avoid these errors. These alternatives also provide more powerful, flexible and extensible approaches to formatting text.
2744
2745
[_Return to the Top_](#python-cheatsheet)
2746
2747
### Lazy string formatting
2748
2749
You would only use `%s` string formatting on functions that can do lazy parameters evaluation,
2750
the most common being logging:
2751
2752
Prefer:
2753
2754
```python
2755
>>> name = "alice"
2756
>>> logging.debug("User name: %s", name)
2757
```
2758
2759
Over:
2760
2761
```python
2762
>>> logging.debug("User name: {}".format(name))
2763
```
2764
2765
Or:
2766
2767
```python
2768
>>> logging.debug("User name: " + name)
2769
```
2770
2771
[_Return to the Top_](#python-cheatsheet)
2772
2773
### Formatted String Literals or f-strings (Python 3.6+)
2774
2775
```python
2776
>>> name = 'Elizabeth'
2777
>>> f'Hello {name}!'
2778
'Hello Elizabeth!
2779
```
2780
2781
It is even possible to do inline arithmetic with it:
2782
2783
```python
2784
>>> a = 5
2785
>>> b = 10
2786
>>> f'Five plus ten is {a + b} and not {2 * (a + b)}.'
2787
'Five plus ten is 15 and not 30.'
2788
```
2789
2790
[_Return to the Top_](#python-cheatsheet)
2791
2792
### Template Strings
2793
2794
A simpler and less powerful mechanism, but it is recommended when handling format strings generated by users. Due to their reduced complexity template strings are a safer choice.
2795
2796
```python
2797
>>> from string import Template
2798
>>> name = 'Elizabeth'
2799
>>> t = Template('Hey $name!')
2800
>>> t.substitute(name=name)
2801
'Hey Elizabeth!'
2802
```
2803
2804
[_Return to the Top_](#python-cheatsheet)
2805
2806
## Regular Expressions
2807
2808
1. Import the regex module with `import re`.
2809
1. Create a Regex object with the `re.compile()` function. (Remember to use a raw string.)
2810
1. Pass the string you want to search into the Regex object’s `search()` method. This returns a `Match` object.
2811
1. Call the Match object’s `group()` method to return a string of the actual matched text.
2812
2813
All the regex functions in Python are in the re module:
2814
2815
```python
2816
>>> import re
2817
```
2818
2819
[_Return to the Top_](#python-cheatsheet)
2820
2821
### Matching Regex Objects
2822
2823
```python
2824
>>> phone_num_regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d')
2825
2826
>>> mo = phone_num_regex.search('My number is 415-555-4242.')
2827
2828
>>> print('Phone number found: {}'.format(mo.group()))
2829
Phone number found: 415-555-4242
2830
```
2831
2832
[_Return to the Top_](#python-cheatsheet)
2833
2834
### Grouping with Parentheses
2835
2836
```python
2837
>>> phone_num_regex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
2838
2839
>>> mo = phone_num_regex.search('My number is 415-555-4242.')
2840
2841
>>> mo.group(1)
2842
'415'
2843
2844
>>> mo.group(2)
2845
'555-4242'
2846
2847
>>> mo.group(0)
2848
'415-555-4242'
2849
2850
>>> mo.group()
2851
'415-555-4242'
2852
```
2853
2854
To retrieve all the groups at once: use the groups() method—note the plural form for the name.
2855
2856
```python
2857
>>> mo.groups()
2858
('415', '555-4242')
2859
2860
>>> area_code, main_number = mo.groups()
2861
2862
>>> print(area_code)
2863
415
2864
2865
>>> print(main_number)
2866
555-4242
2867
```
2868
2869
[_Return to the Top_](#python-cheatsheet)
2870
2871
### Matching Multiple Groups with the Pipe
2872
2873
The | character is called a pipe. You can use it anywhere you want to match one of many expressions. For example, the regular expression r'Batman|Tina Fey' will match either 'Batman' or 'Tina Fey'.
2874
2875
```python
2876
>>> hero_regex = re.compile (r'Batman|Tina Fey')
2877
2878
>>> mo1 = hero_regex.search('Batman and Tina Fey.')
2879
2880
>>> mo1.group()
2881
'Batman'
2882
2883
>>> mo2 = hero_regex.search('Tina Fey and Batman.')
2884
2885
>>> mo2.group()
2886
'Tina Fey'
2887
```
2888
2889
You can also use the pipe to match one of several patterns as part of your regex:
2890
2891
```python
2892
>>> bat_regex = re.compile(r'Bat(man|mobile|copter|bat)')
2893
2894
>>> mo = bat_regex.search('Batmobile lost a wheel')
2895
2896
>>> mo.group()
2897
'Batmobile'
2898
2899
>>> mo.group(1)
2900
'mobile'
2901
```
2902
2903
[_Return to the Top_](#python-cheatsheet)
2904
2905
### Optional Matching with the Question Mark
2906
2907
The ? character flags the group that precedes it as an optional part of the pattern.
2908
2909
```python
2910
>>> bat_regex = re.compile(r'Bat(wo)?man')
2911
>>> mo1 = bat_regex.search('The Adventures of Batman')
2912
>>> mo1.group()
2913
'Batman'
2914
2915
>>> mo2 = bat_regex.search('The Adventures of Batwoman')
2916
>>> mo2.group()
2917
'Batwoman'
2918
```
2919
2920
[_Return to the Top_](#python-cheatsheet)
2921
2922
### Matching Zero or More with the Star
2923
2924
The \* (called the star or asterisk) means “match zero or more”—the group that precedes the star can occur any number of times in the text.
2925
2926
```python
2927
>>> bat_regex = re.compile(r'Bat(wo)*man')
2928
>>> mo1 = bat_regex.search('The Adventures of Batman')
2929
>>> mo1.group()
2930
'Batman'
2931
2932
>>> mo2 = bat_regex.search('The Adventures of Batwoman')
2933
>>> mo2.group()
2934
'Batwoman'
2935
2936
>>> mo3 = bat_regex.search('The Adventures of Batwowowowoman')
2937
>>> mo3.group()
2938
'Batwowowowoman'
2939
```
2940
2941
[_Return to the Top_](#python-cheatsheet)
2942
2943
### Matching One or More with the Plus
2944
2945
While \* means “match zero or more,” the + (or plus) means “match one or more”. The group preceding a plus must appear at least once. It is not optional:
2946
2947
```python
2948
>>> bat_regex = re.compile(r'Bat(wo)+man')
2949
>>> mo1 = bat_regex.search('The Adventures of Batwoman')
2950
>>> mo1.group()
2951
'Batwoman'
2952
```
2953
2954
```python
2955
>>> mo2 = bat_regex.search('The Adventures of Batwowowowoman')
2956
>>> mo2.group()
2957
'Batwowowowoman'
2958
```
2959
2960
```python
2961
>>> mo3 = bat_regex.search('The Adventures of Batman')
2962
>>> mo3 is None
2963
True
2964
```
2965
2966
[_Return to the Top_](#python-cheatsheet)
2967
2968
### Matching Specific Repetitions with Curly Brackets
2969
2970
If you have a group that you want to repeat a specific number of times, follow the group in your regex with a number in curly brackets. For example, the regex (Ha){3} will match the string 'HaHaHa', but it will not match 'HaHa', since the latter has only two repeats of the (Ha) group.
2971
2972
Instead of one number, you can specify a range by writing a minimum, a comma, and a maximum in between the curly brackets. For example, the regex (Ha){3,5} will match 'HaHaHa', 'HaHaHaHa', and 'HaHaHaHaHa'.
2973
2974
```python
2975
>>> ha_regex = re.compile(r'(Ha){3}')
2976
>>> mo1 = ha_regex.search('HaHaHa')
2977
>>> mo1.group()
2978
'HaHaHa'
2979
```
2980
2981
```python
2982
>>> mo2 = ha_regex.search('Ha')
2983
>>> mo2 is None
2984
True
2985
```
2986
2987
[_Return to the Top_](#python-cheatsheet)
2988
2989
### Greedy and Nongreedy Matching
2990
2991
Python’s regular expressions are greedy by default, which means that in ambiguous situations they will match the longest string possible. The non-greedy version of the curly brackets, which matches the shortest string possible, has the closing curly bracket followed by a question mark.
2992
2993
```python
2994
>>> greedy_ha_regex = re.compile(r'(Ha){3,5}')
2995
>>> mo1 = greedy_ha_regex.search('HaHaHaHaHa')
2996
>>> mo1.group()
2997
'HaHaHaHaHa'
2998
```
2999
3000
```python
3001
>>> nongreedy_ha_regex = re.compile(r'(Ha){3,5}?')
3002
>>> mo2 = nongreedy_ha_regex.search('HaHaHaHaHa')
3003
>>> mo2.group()
3004
'HaHaHa'
3005
```
3006
3007
[_Return to the Top_](#python-cheatsheet)
3008
3009
### The findall() Method
3010
3011
In addition to the search() method, Regex objects also have a findall() method. While search() will return a Match object of the first matched text in the searched string, the findall() method will return the strings of every match in the searched string.
3012
3013
```python
3014
>>> phone_num_regex = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') # has no groups
3015
3016
>>> phone_num_regex.findall('Cell: 415-555-9999 Work: 212-555-0000')
3017
['415-555-9999', '212-555-0000']
3018
```
3019
3020
To summarize what the findall() method returns, remember the following:
3021
3022
- When called on a regex with no groups, such as \d-\d\d\d-\d\d\d\d, the method findall() returns a list of ng matches, such as ['415-555-9999', '212-555-0000'].
3023
3024
- When called on a regex that has groups, such as (\d\d\d)-(d\d)-(\d\d\d\d), the method findall() returns a list of es of strings (one string for each group), such as [('415', '555', '9999'), ('212', '555', '0000')].
3025
3026
[_Return to the Top_](#python-cheatsheet)
3027
3028
### Making Your Own Character Classes
3029
3030
There are times when you want to match a set of characters but the shorthand character classes (\d, \w, \s, and so on) are too broad. You can define your own character class using square brackets. For example, the character class [aeiouAEIOU] will match any vowel, both lowercase and uppercase.
3031
3032
```python
3033
>>> vowel_regex = re.compile(r'[aeiouAEIOU]')
3034
3035
>>> vowel_regex.findall('Robocop eats baby food. BABY FOOD.')
3036
['o', 'o', 'o', 'e', 'a', 'a', 'o', 'o', 'A', 'O', 'O']
3037
```
3038
3039
You can also include ranges of letters or numbers by using a hyphen. For example, the character class [a-zA-Z0-9] will match all lowercase letters, uppercase letters, and numbers.
3040
3041
By placing a caret character (^) just after the character class’s opening bracket, you can make a negative character class. A negative character class will match all the characters that are not in the character class. For example, enter the following into the interactive shell:
3042
3043
```python
3044
>>> consonant_regex = re.compile(r'[^aeiouAEIOU]')
3045
3046
>>> consonant_regex.findall('Robocop eats baby food. BABY FOOD.')
3047
['R', 'b', 'c', 'p', ' ', 't', 's', ' ', 'b', 'b', 'y', ' ', 'f', 'd', '.', '
3048
', 'B', 'B', 'Y', ' ', 'F', 'D', '.']
3049
```
3050
3051
[_Return to the Top_](#python-cheatsheet)
3052
3053
### The Caret and Dollar Sign Characters
3054
3055
- You can also use the caret symbol (^) at the start of a regex to indicate that a match must occur at the beginning of the searched text.
3056
3057
- Likewise, you can put a dollar sign (\$) at the end of the regex to indicate the string must end with this regex pattern.
3058
3059
- And you can use the ^ and \$ together to indicate that the entire string must match the regex—that is, it’s not enough for a match to be made on some subset of the string.
3060
3061
The r'^Hello' regular expression string matches strings that begin with 'Hello':
3062
3063
```python
3064
>>> begins_with_hello = re.compile(r'^Hello')
3065
3066
>>> begins_with_hello.search('Hello world!')
3067
<_sre.SRE_Match object; span=(0, 5), match='Hello'>
3068
3069
>>> begins_with_hello.search('He said hello.') is None
3070
True
3071
```
3072
3073
The r'\d\#x27; regular expression string matches strings that end with a numeric character from 0 to 9:
3074
3075
```python
3076
>>> whole_string_is_num = re.compile(r'^\d+#x27;)
3077
3078
>>> whole_string_is_num.search('1234567890')
3079
<_sre.SRE_Match object; span=(0, 10), match='1234567890'>
3080
3081
>>> whole_string_is_num.search('12345xyz67890') is None
3082
True
3083
3084
>>> whole_string_is_num.search('12 34567890') is None
3085
True
3086
```
3087
3088
[_Return to the Top_](#python-cheatsheet)
3089
3090
### The Wildcard Character
3091
3092
The . (or dot) character in a regular expression is called a wildcard and will match any character except for a newline:
3093
3094
```python
3095
>>> at_regex = re.compile(r'.at')
3096
3097
>>> at_regex.findall('The cat in the hat sat on the flat mat.')
3098
['cat', 'hat', 'sat', 'lat', 'mat']
3099
```
3100
3101
[_Return to the Top_](#python-cheatsheet)
3102
3103
### Matching Everything with Dot-Star
3104
3105
```python
3106
>>> name_regex = re.compile(r'First Name: (.*) Last Name: (.*)')
3107
3108
>>> mo = name_regex.search('First Name: Al Last Name: Sweigart')
3109
3110
>>> mo.group(1)
3111
'Al'
3112
```
3113
3114
```python
3115
>>> mo.group(2)
3116
'Sweigart'
3117
```
3118
3119
The dot-star uses greedy mode: It will always try to match as much text as possible. To match any and all text in a nongreedy fashion, use the dot, star, and question mark (.\*?). The question mark tells Python to match in a nongreedy way:
3120
3121
```python
3122
>>> nongreedy_regex = re.compile(r'<.*?>')
3123
>>> mo = nongreedy_regex.search('<To serve man> for dinner.>')
3124
>>> mo.group()
3125
'<To serve man>'
3126
```
3127
3128
```python
3129
>>> greedy_regex = re.compile(r'<.*>')
3130
>>> mo = greedy_regex.search('<To serve man> for dinner.>')
3131
>>> mo.group()
3132
'<To serve man> for dinner.>'
3133
```
3134
3135
[_Return to the Top_](#python-cheatsheet)
3136
3137
### Matching Newlines with the Dot Character
3138
3139
The dot-star will match everything except a newline. By passing re.DOTALL as the second argument to re.compile(), you can make the dot character match all characters, including the newline character:
3140
3141
```python
3142
>>> no_newline_regex = re.compile('.*')
3143
>>> no_newline_regex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()
3144
'Serve the public trust.'
3145
```
3146
3147
```python
3148
>>> newline_regex = re.compile('.*', re.DOTALL)
3149
>>> newline_regex.search('Serve the public trust.\nProtect the innocent.\nUphold the law.').group()
3150
'Serve the public trust.\nProtect the innocent.\nUphold the law.'
3151
```
3152
3153
[_Return to the Top_](#python-cheatsheet)
3154
3155
### Review of Regex Symbols
3156
3157
| Symbol | Matches |
3158
| ------------------------ | ------------------------------------------------------ |
3159
| `?` | zero or one of the preceding group. |
3160
| `*` | zero or more of the preceding group. |
3161
| `+` | one or more of the preceding group. |
3162
| `{n}` | exactly n of the preceding group. |
3163
| `{n,}` | n or more of the preceding group. |
3164
| `{,m}` | 0 to m of the preceding group. |
3165
| `{n,m}` | at least n and at most m of the preceding p. |
3166
| `{n,m}?` or `*?` or `+?` | performs a nongreedy match of the preceding p. |
3167
| `^spam` | means the string must begin with spam. |
3168
| `spam Cheatsheet-v2 - datastructures-in-python | means the string must end with spam. |
3169
| `.` | any character, except newline characters. |
3170
| `\d`, `\w`, and `\s` | a digit, word, or space character, respectively. |
3171
| `\D`, `\W`, and `\S` | anything except a digit, word, or space, respectively. |
3172
| `[abc]` | any character between the brackets (such as a, b, ). |
3173
| `[^abc]` | any character that isn’t between the brackets. |
3174
3175
[_Return to the Top_](#python-cheatsheet)
3176
3177
### Case-Insensitive Matching
3178
3179
To make your regex case-insensitive, you can pass re.IGNORECASE or re.I as a second argument to re.compile():
3180
3181
```python
3182
>>> robocop = re.compile(r'robocop', re.I)
3183
3184
>>> robocop.search('Robocop is part man, part machine, all cop.').group()
3185
'Robocop'
3186
```
3187
3188
```python
3189
>>> robocop.search('ROBOCOP protects the innocent.').group()
3190
'ROBOCOP'
3191
```
3192
3193
```python
3194
>>> robocop.search('Al, why does your programming book talk about robocop so much?').group()
3195
'robocop'
3196
```
3197
3198
[_Return to the Top_](#python-cheatsheet)
3199
3200
### Substituting Strings with the sub() Method
3201
3202
The sub() method for Regex objects is passed two arguments:
3203
3204
1. The first argument is a string to replace any matches.
3205
1. The second is the string for the regular expression.
3206
3207
The sub() method returns a string with the substitutions applied:
3208
3209
```python
3210
>>> names_regex = re.compile(r'Agent \w+')
3211
3212
>>> names_regex.sub('CENSORED', 'Agent Alice gave the secret documents to Agent Bob.')
3213
'CENSORED gave the secret documents to CENSORED.'
3214
```
3215
3216
Another example:
3217
3218
```python
3219
>>> agent_names_regex = re.compile(r'Agent (\w)\w*')
3220
3221
>>> agent_names_regex.sub(r'\1****', 'Agent Alice told Agent Carol that Agent Eve knew Agent Bob was a double agent.')
3222
A**** told C**** that E**** knew B**** was a double agent.'
3223
```
3224
3225
[_Return to the Top_](#python-cheatsheet)
3226
3227
### Managing Complex Regexes
3228
3229
To tell the re.compile() function to ignore whitespace and comments inside the regular expression string, “verbose mode” can be enabled by passing the variable re.VERBOSE as the second argument to re.compile().
3230
3231
Now instead of a hard-to-read regular expression like this:
3232
3233
```python
3234
phone_regex = re.compile(r'((\d{3}|\(\d{3}\))?(\s|-|\.)?\d{3}(\s|-|\.)\d{4}(\s*(ext|x|ext.)\s*\d{2,5})?)')
3235
```
3236
3237
you can spread the regular expression over multiple lines with comments like this:
3238
3239
```python
3240
phone_regex = re.compile(r'''(
3241
(\d{3}|\(\d{3}\))? # area code
3242
(\s|-|\.)? # separator
3243
\d{3} # first 3 digits
3244
(\s|-|\.) # separator
3245
\d{4} # last 4 digits
3246
(\s*(ext|x|ext.)\s*\d{2,5})? # extension
3247
)''', re.VERBOSE)
3248
```
3249
3250
[_Return to the Top_](#python-cheatsheet)
3251
3252
## Handling File and Directory Paths
3253
3254
There are two main modules in Python that deals with path manipulation.
3255
One is the `os.path` module and the other is the `pathlib` module.
3256
The `pathlib` module was added in Python 3.4, offering an object-oriented way
3257
to handle file system paths.
3258
3259
[_Return to the Top_](#python-cheatsheet)
3260
3261
### Backslash on Windows and Forward Slash on OS X and Linux
3262
3263
On Windows, paths are written using backslashes (`\`) as the separator between
3264
folder names. On Unix based operating system such as macOS, Linux, and BSDs,
3265
the forward slash (`/`) is used as the path separator. Joining paths can be
3266
a headache if your code needs to work on different platforms.
3267
3268
Fortunately, Python provides easy ways to handle this. We will showcase
3269
how to deal with this with both `os.path.join` and `pathlib.Path.joinpath`
3270
3271
Using `os.path.join` on Windows:
3272
3273
```python
3274
>>> import os
3275
3276
>>> os.path.join('usr', 'bin', 'spam')
3277
'usr\\bin\\spam'
3278
```
3279
3280
And using `pathlib` on \*nix:
3281
3282
```python
3283
>>> from pathlib import Path
3284
3285
>>> print(Path('usr').joinpath('bin').joinpath('spam'))
3286
usr/bin/spam
3287
```
3288
3289
`pathlib` also provides a shortcut to joinpath using the `/` operator:
3290
3291
```python
3292
>>> from pathlib import Path
3293
3294
>>> print(Path('usr') / 'bin' / 'spam')
3295
usr/bin/spam
3296
```
3297
3298
Notice the path separator is different between Windows and Unix based operating
3299
system, that's why you want to use one of the above methods instead of
3300
adding strings together to join paths together.
3301
3302
Joining paths is helpful if you need to create different file paths under
3303
the same directory.
3304
3305
Using `os.path.join` on Windows:
3306
3307
```python
3308
>>> my_files = ['accounts.txt', 'details.csv', 'invite.docx']
3309
3310
>>> for filename in my_files:
3311
>>> print(os.path.join('C:\\Users\\asweigart', filename))
3312
C:\Users\asweigart\accounts.txt
3313
C:\Users\asweigart\details.csv
3314
C:\Users\asweigart\invite.docx
3315
```
3316
3317
Using `pathlib` on \*nix:
3318
3319
```python
3320
>>> my_files = ['accounts.txt', 'details.csv', 'invite.docx']
3321
>>> home = Path.home()
3322
>>> for filename in my_files:
3323
>>> print(home / filename)
3324
/home/asweigart/accounts.txt
3325
/home/asweigart/details.csv
3326
/home/asweigart/invite.docx
3327
```
3328
3329
[_Return to the Top_](#python-cheatsheet)
3330
3331
### The Current Working Directory
3332
3333
Using `os` on Windows:
3334
3335
```python
3336
>>> import os
3337
3338
>>> os.getcwd()
3339
'C:\\Python34'
3340
>>> os.chdir('C:\\Windows\\System32')
3341
3342
>>> os.getcwd()
3343
'C:\\Windows\\System32'
3344
```
3345
3346
Using `pathlib` on \*nix:
3347
3348
```python
3349
>>> from pathlib import Path
3350
>>> from os import chdir
3351
3352
>>> print(Path.cwd())
3353
/home/asweigart
3354
3355
>>> chdir('/usr/lib/python3.6')
3356
>>> print(Path.cwd())
3357
/usr/lib/python3.6
3358
```
3359
3360
[_Return to the Top_](#python-cheatsheet)
3361
3362
### Creating New Folders
3363
3364
Using `os` on Windows:
3365
3366
```python
3367
>>> import os
3368
>>> os.makedirs('C:\\delicious\\walnut\\waffles')
3369
```
3370
3371
Using `pathlib` on \*nix:
3372
3373
```python
3374
>>> from pathlib import Path
3375
>>> cwd = Path.cwd()
3376
>>> (cwd / 'delicious' / 'walnut' / 'waffles').mkdir()
3377
Traceback (most recent call last):
3378
File "<stdin>", line 1, in <module>
3379
File "/usr/lib/python3.6/pathlib.py", line 1226, in mkdir
3380
self._accessor.mkdir(self, mode)
3381
File "/usr/lib/python3.6/pathlib.py", line 387, in wrapped
3382
return strfunc(str(pathobj), *args)
3383
FileNotFoundError: [Errno 2] No such file or directory: '/home/asweigart/delicious/walnut/waffles'
3384
```
3385
3386
Oh no, we got a nasty error! The reason is that the 'delicious' directory does
3387
not exist, so we cannot make the 'walnut' and the 'waffles' directories under
3388
it. To fix this, do:
3389
3390
```python
3391
>>> from pathlib import Path
3392
>>> cwd = Path.cwd()
3393
>>> (cwd / 'delicious' / 'walnut' / 'waffles').mkdir(parents=True)
3394
```
3395
3396
And all is good :)
3397
3398
[_Return to the Top_](#python-cheatsheet)
3399
3400
### Absolute vs. Relative Paths
3401
3402
There are two ways to specify a file path.
3403
3404
- An absolute path, which always begins with the root folder
3405
- A relative path, which is relative to the program’s current working directory
3406
3407
There are also the dot (.) and dot-dot (..) folders. These are not real folders but special names that can be used in a path. A single period (“dot”) for a folder name is shorthand for “this directory.” Two periods (“dot-dot”) means “the parent folder.”
3408
3409
[_Return to the Top_](#python-cheatsheet)
3410
3411
### Handling Absolute and Relative Paths
3412
3413
To see if a path is an absolute path:
3414
3415
Using `os.path` on \*nix:
3416
3417
```python
3418
>>> import os
3419
>>> os.path.isabs('/')
3420
True
3421
>>> os.path.isabs('..')
3422
False
3423
```
3424
3425
Using `pathlib` on \*nix:
3426
3427
```python
3428
>>> from pathlib import Path
3429
>>> Path('/').is_absolute()
3430
True
3431
>>> Path('..').is_absolute()
3432
False
3433
```
3434
3435
You can extract an absolute path with both `os.path` and `pathlib`
3436
3437
Using `os.path` on \*nix:
3438
3439
```python
3440
>>> import os
3441
>>> os.getcwd()
3442
'/home/asweigart'
3443
>>> os.path.abspath('..')
3444
'/home'
3445
```
3446
3447
Using `pathlib` on \*nix:
3448
3449
```python
3450
from pathlib import Path
3451
print(Path.cwd())
3452
/home/asweigart
3453
print(Path('..').resolve())
3454
/home
3455
```
3456
3457
You can get a relative path from a starting path to another path.
3458
3459
Using `os.path` on \*nix:
3460
3461
```python
3462
>>> import os
3463
>>> os.path.relpath('/etc/passwd', '/')
3464
'etc/passwd'
3465
```
3466
3467
Using `pathlib` on \*nix:
3468
3469
```python
3470
>>> from pathlib import Path
3471
>>> print(Path('/etc/passwd').relative_to('/'))
3472
etc/passwd
3473
```
3474
3475
[_Return to the Top_](#python-cheatsheet)
3476
3477
### Checking Path Validity
3478
3479
Checking if a file/directory exists:
3480
3481
Using `os.path` on \*nix:
3482
3483
```python
3484
import os
3485
>>> os.path.exists('.')
3486
True
3487
>>> os.path.exists('setup.py')
3488
True
3489
>>> os.path.exists('/etc')
3490
True
3491
>>> os.path.exists('nonexistentfile')
3492
False
3493
```
3494
3495
Using `pathlib` on \*nix:
3496
3497
```python
3498
from pathlib import Path
3499
>>> Path('.').exists()
3500
True
3501
>>> Path('setup.py').exists()
3502
True
3503
>>> Path('/etc').exists()
3504
True
3505
>>> Path('nonexistentfile').exists()
3506
False
3507
```
3508
3509
Checking if a path is a file:
3510
3511
Using `os.path` on \*nix:
3512
3513
```python
3514
>>> import os
3515
>>> os.path.isfile('setup.py')
3516
True
3517
>>> os.path.isfile('/home')
3518
False
3519
>>> os.path.isfile('nonexistentfile')
3520
False
3521
```
3522
3523
Using `pathlib` on \*nix:
3524
3525
```python
3526
>>> from pathlib import Path
3527
>>> Path('setup.py').is_file()
3528
True
3529
>>> Path('/home').is_file()
3530
False
3531
>>> Path('nonexistentfile').is_file()
3532
False
3533
```
3534
3535
Checking if a path is a directory:
3536
3537
Using `os.path` on \*nix:
3538
3539
```python
3540
>>> import os
3541
>>> os.path.isdir('/')
3542
True
3543
>>> os.path.isdir('setup.py')
3544
False
3545
>>> os.path.isdir('/spam')
3546
False
3547
```
3548
3549
Using `pathlib` on \*nix:
3550
3551
```python
3552
>>> from pathlib import Path
3553
>>> Path('/').is_dir()
3554
True
3555
>>> Path('setup.py').is_dir()
3556
False
3557
>>> Path('/spam').is_dir()
3558
False
3559
```
3560
3561
[_Return to the Top_](#python-cheatsheet)
3562
3563
### Finding File Sizes and Folder Contents
3564
3565
Getting a file's size in bytes:
3566
3567
Using `os.path` on Windows:
3568
3569
```python
3570
>>> import os
3571
>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')
3572
776192
3573
```
3574
3575
Using `pathlib` on \*nix:
3576
3577
```python
3578
>>> from pathlib import Path
3579
>>> stat = Path('/bin/python3.6').stat()
3580
>>> print(stat) # stat contains some other information about the file as well
3581
os.stat_result(st_mode=33261, st_ino=141087, st_dev=2051, st_nlink=2, st_uid=0,
3582
--snip--
3583
st_gid=0, st_size=10024, st_atime=1517725562, st_mtime=1515119809, st_ctime=1517261276)
3584
>>> print(stat.st_size) # size in bytes
3585
10024
3586
```
3587
3588
Listing directory contents using `os.listdir` on Windows:
3589
3590
```python
3591
>>> import os
3592
>>> os.listdir('C:\\Windows\\System32')
3593
['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',
3594
--snip--
3595
'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']
3596
```
3597
3598
Listing directory contents using `pathlib` on \*nix:
3599
3600
```python
3601
>>> from pathlib import Path
3602
>>> for f in Path('/usr/bin').iterdir():
3603
>>> print(f)
3604
...
3605
/usr/bin/tiff2rgba
3606
/usr/bin/iconv
3607
/usr/bin/ldd
3608
/usr/bin/cache_restore
3609
/usr/bin/udiskie
3610
/usr/bin/unix2dos
3611
/usr/bin/t1reencode
3612
/usr/bin/epstopdf
3613
/usr/bin/idle3
3614
...
3615
```
3616
3617
To find the total size of all the files in this directory:
3618
3619
**WARNING**: Directories themselves also have a size! So you might want to
3620
check for whether a path is a file or directory using the methods in the methods discussed in the above section!
3621
3622
Using `os.path.getsize()` and `os.listdir()` together on Windows:
3623
3624
```python
3625
>>> import os
3626
>>> total_size = 0
3627
3628
>>> for filename in os.listdir('C:\\Windows\\System32'):
3629
total_size = total_size + os.path.getsize(os.path.join('C:\\Windows\\System32', filename))
3630
3631
>>> print(total_size)
3632
1117846456
3633
```
3634
3635
Using `pathlib` on \*nix:
3636
3637
```python
3638
>>> from pathlib import Path
3639
>>> total_size = 0
3640
3641
>>> for sub_path in Path('/usr/bin').iterdir():
3642
... total_size += sub_path.stat().st_size
3643
>>>
3644
>>> print(total_size)
3645
1903178911
3646
```
3647
3648
[_Return to the Top_](#python-cheatsheet)
3649
3650
### Copying Files and Folders
3651
3652
The shutil module provides functions for copying files, as well as entire folders.
3653
3654
```python
3655
>>> import shutil, os
3656
3657
>>> os.chdir('C:\\')
3658
3659
>>> shutil.copy('C:\\spam.txt', 'C:\\delicious')
3660
'C:\\delicious\\spam.txt'
3661
3662
>>> shutil.copy('eggs.txt', 'C:\\delicious\\eggs2.txt')
3663
'C:\\delicious\\eggs2.txt'
3664
```
3665
3666
While shutil.copy() will copy a single file, shutil.copytree() will copy an entire folder and every folder and file contained in it:
3667
3668
```python
3669
>>> import shutil, os
3670
3671
>>> os.chdir('C:\\')
3672
3673
>>> shutil.copytree('C:\\bacon', 'C:\\bacon_backup')
3674
'C:\\bacon_backup'
3675
```
3676
3677
[_Return to the Top_](#python-cheatsheet)
3678
3679
### Moving and Renaming Files and Folders
3680
3681
```python
3682
>>> import shutil
3683
>>> shutil.move('C:\\bacon.txt', 'C:\\eggs')
3684
'C:\\eggs\\bacon.txt'
3685
```
3686
3687
The destination path can also specify a filename. In the following example, the source file is moved and renamed:
3688
3689
```python
3690
>>> shutil.move('C:\\bacon.txt', 'C:\\eggs\\new_bacon.txt')
3691
'C:\\eggs\\new_bacon.txt'
3692
```
3693
3694
If there is no eggs folder, then move() will rename bacon.txt to a file named eggs.
3695
3696
```python
3697
>>> shutil.move('C:\\bacon.txt', 'C:\\eggs')
3698
'C:\\eggs'
3699
```
3700
3701
[_Return to the Top_](#python-cheatsheet)
3702
3703
### Permanently Deleting Files and Folders
3704
3705
- Calling os.unlink(path) or Path.unlink() will delete the file at path.
3706
3707
- Calling os.rmdir(path) or Path.rmdir() will delete the folder at path. This folder must be empty of any files or folders.
3708
3709
- Calling shutil.rmtree(path) will remove the folder at path, and all files and folders it contains will also be deleted.
3710
3711
[_Return to the Top_](#python-cheatsheet)
3712
3713
### Safe Deletes with the send2trash Module
3714
3715
You can install this module by running pip install send2trash from a Terminal window.
3716
3717
```python
3718
>>> import send2trash
3719
3720
>>> with open('bacon.txt', 'a') as bacon_file: # creates the file
3721
... bacon_file.write('Bacon is not a vegetable.')
3722
25
3723
3724
>>> send2trash.send2trash('bacon.txt')
3725
```
3726
3727
[_Return to the Top_](#python-cheatsheet)
3728
3729
### Walking a Directory Tree
3730
3731
```python
3732
>>> import os
3733
>>>
3734
>>> for folder_name, subfolders, filenames in os.walk('C:\\delicious'):
3735
>>> print('The current folder is {}'.format(folder_name))
3736
>>>
3737
>>> for subfolder in subfolders:
3738
>>> print('SUBFOLDER OF {}: {}'.format(folder_name, subfolder))
3739
>>> for filename in filenames:
3740
>>> print('FILE INSIDE {}: {}'.format(folder_name, filename))
3741
>>>
3742
>>> print('')
3743
The current folder is C:\delicious
3744
SUBFOLDER OF C:\delicious: cats
3745
SUBFOLDER OF C:\delicious: walnut
3746
FILE INSIDE C:\delicious: spam.txt
3747
3748
The current folder is C:\delicious\cats
3749
FILE INSIDE C:\delicious\cats: catnames.txt
3750
FILE INSIDE C:\delicious\cats: zophie.jpg
3751
3752
The current folder is C:\delicious\walnut
3753
SUBFOLDER OF C:\delicious\walnut: waffles
3754
3755
The current folder is C:\delicious\walnut\waffles
3756
FILE INSIDE C:\delicious\walnut\waffles: butter.txt
3757
```
3758
3759
[_Return to the Top_](#python-cheatsheet)
3760
3761
`pathlib` provides a lot more functionality than the ones listed above,
3762
like getting file name, getting file extension, reading/writing a file without
3763
manually opening it, etc. Check out the
3764
[official documentation](https://docs.python.org/3/library/pathlib.html)
3765
if you want to know more!
3766
3767
## Reading and Writing Files
3768
3769
### The File Reading/Writing Process
3770
3771
To read/write to a file in Python, you will want to use the `with`
3772
statement, which will close the file for you after you are done.
3773
3774
[_Return to the Top_](#python-cheatsheet)
3775
3776
### Opening and reading files with the open() function
3777
3778
```python
3779
>>> with open('C:\\Users\\your_home_folder\\hello.txt') as hello_file:
3780
... hello_content = hello_file.read()
3781
>>> hello_content
3782
'Hello World!'
3783
3784
>>> # Alternatively, you can use the *readlines()* method to get a list of string values from the file, one string for each line of text:
3785
3786
>>> with open('sonnet29.txt') as sonnet_file:
3787
... sonnet_file.readlines()
3788
[When, in disgrace with fortune and men's eyes,\n', ' I all alone beweep my
3789
outcast state,\n', And trouble deaf heaven with my bootless cries,\n', And
3790
look upon myself and curse my fate,']
3791
3792
>>> # You can also iterate through the file line by line:
3793
>>> with open('sonnet29.txt') as sonnet_file:
3794
... for line in sonnet_file: # note the new line character will be included in the line
3795
... print(line, end='')
3796
3797
When, in disgrace with fortune and men's eyes,
3798
I all alone beweep my outcast state,
3799
And trouble deaf heaven with my bootless cries,
3800
And look upon myself and curse my fate,
3801
```
3802
3803
[_Return to the Top_](#python-cheatsheet)
3804
3805
### Writing to Files
3806
3807
```python
3808
>>> with open('bacon.txt', 'w') as bacon_file:
3809
... bacon_file.write('Hello world!\n')
3810
13
3811
3812
>>> with open('bacon.txt', 'a') as bacon_file:
3813
... bacon_file.write('Bacon is not a vegetable.')
3814
25
3815
3816
>>> with open('bacon.txt') as bacon_file:
3817
... content = bacon_file.read()
3818
3819
>>> print(content)
3820
Hello world!
3821
Bacon is not a vegetable.
3822
```
3823
3824
[_Return to the Top_](#python-cheatsheet)
3825
3826
### Saving Variables with the shelve Module
3827
3828
To save variables:
3829
3830
```python
3831
>>> import shelve
3832
3833
>>> cats = ['Zophie', 'Pooka', 'Simon']
3834
>>> with shelve.open('mydata') as shelf_file:
3835
... shelf_file['cats'] = cats
3836
```
3837
3838
To open and read variables:
3839
3840
```python
3841
>>> with shelve.open('mydata') as shelf_file:
3842
... print(type(shelf_file))
3843
... print(shelf_file['cats'])
3844
<class 'shelve.DbfilenameShelf'>
3845
['Zophie', 'Pooka', 'Simon']
3846
```
3847
3848
Just like dictionaries, shelf values have keys() and values() methods that will return list-like values of the keys and values in the shelf. Since these methods return list-like values instead of true lists, you should pass them to the list() function to get them in list form.
3849
3850
```python
3851
>>> with shelve.open('mydata') as shelf_file:
3852
... print(list(shelf_file.keys()))
3853
... print(list(shelf_file.values()))
3854
['cats']
3855
[['Zophie', 'Pooka', 'Simon']]
3856
```
3857
3858
[_Return to the Top_](#python-cheatsheet)
3859
3860
### Saving Variables with the pprint.pformat() Function
3861
3862
```python
3863
>>> import pprint
3864
3865
>>> cats = [{'name': 'Zophie', 'desc': 'chubby'}, {'name': 'Pooka', 'desc': 'fluffy'}]
3866
3867
>>> pprint.pformat(cats)
3868
"[{'desc': 'chubby', 'name': 'Zophie'}, {'desc': 'fluffy', 'name': 'Pooka'}]"
3869
3870
>>> with open('myCats.py', 'w') as file_obj:
3871
... file_obj.write('cats = {}\n'.format(pprint.pformat(cats)))
3872
83
3873
```
3874
3875
[_Return to the Top_](#python-cheatsheet)
3876
3877
### Reading ZIP Files
3878
3879
```python
3880
>>> import zipfile, os
3881
3882
>>> os.chdir('C:\\') # move to the folder with example.zip
3883
>>> with zipfile.ZipFile('example.zip') as example_zip:
3884
... print(example_zip.namelist())
3885
... spam_info = example_zip.getinfo('spam.txt')
3886
... print(spam_info.file_size)
3887
... print(spam_info.compress_size)
3888
... print('Compressed file is %sx smaller!' % (round(spam_info.file_size / spam_info.compress_size, 2)))
3889
3890
['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg']
3891
13908
3892
3828
3893
'Compressed file is 3.63x smaller!'
3894
```
3895
3896
[_Return to the Top_](#python-cheatsheet)
3897
3898
### Extracting from ZIP Files
3899
3900
The extractall() method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory.
3901
3902
```python
3903
>>> import zipfile, os
3904
3905
>>> os.chdir('C:\\') # move to the folder with example.zip
3906
3907
>>> with zipfile.ZipFile('example.zip') as example_zip:
3908
... example_zip.extractall()
3909
```
3910
3911
The extract() method for ZipFile objects will extract a single file from the ZIP file. Continue the interactive shell example:
3912
3913
```python
3914
>>> with zipfile.ZipFile('example.zip') as example_zip:
3915
... print(example_zip.extract('spam.txt'))
3916
... print(example_zip.extract('spam.txt', 'C:\\some\\new\\folders'))
3917
'C:\\spam.txt'
3918
'C:\\some\\new\\folders\\spam.txt'
3919
```
3920
3921
[_Return to the Top_](#python-cheatsheet)
3922
3923
### Creating and Adding to ZIP Files
3924
3925
```python
3926
>>> import zipfile
3927
3928
>>> with zipfile.ZipFile('new.zip', 'w') as new_zip:
3929
... new_zip.write('spam.txt', compress_type=zipfile.ZIP_DEFLATED)
3930
```
3931
3932
This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt.
3933
3934
[_Return to the Top_](#python-cheatsheet)
3935
3936
## JSON, YAML and configuration files
3937
3938
### JSON
3939
3940
Open a JSON file with:
3941
3942
```python
3943
import json
3944
with open("filename.json", "r") as f:
3945
content = json.loads(f.read())
3946
```
3947
3948
Write a JSON file with:
3949
3950
```python
3951
import json
3952
3953
content = {"name": "Joe", "age": 20}
3954
with open("filename.json", "w") as f:
3955
f.write(json.dumps(content, indent=2))
3956
```
3957
3958
[_Return to the Top_](#python-cheatsheet)
3959
3960
### YAML
3961
3962
Compared to JSON, YAML allows for much better human maintainability and gives you the option to add comments.
3963
It is a convenient choice for configuration files where humans will have to edit it.
3964
3965
There are two main libraries allowing to access to YAML files:
3966
3967
- [PyYaml](https://pypi.python.org/pypi/PyYAML)
3968
- [Ruamel.yaml](https://pypi.python.org/pypi/ruamel.yaml)
3969
3970
Install them using `pip install` in your virtual environment.
3971
3972
The first one it easier to use but the second one, Ruamel, implements much better the YAML
3973
specification, and allow for example to modify a YAML content without altering comments.
3974
3975
Open a YAML file with:
3976
3977
```python
3978
from ruamel.yaml import YAML
3979
3980
with open("filename.yaml") as f:
3981
yaml=YAML()
3982
yaml.load(f)
3983
```
3984
3985
[_Return to the Top_](#python-cheatsheet)
3986
3987
### Anyconfig
3988
3989
[Anyconfig](https://pypi.python.org/pypi/anyconfig) is a very handy package allowing to abstract completely the underlying configuration file format. It allows to load a Python dictionary from JSON, YAML, TOML, and so on.
3990
3991
Install it with:
3992
3993
```bash
3994
pip install anyconfig
3995
```
3996
3997
Usage:
3998
3999
```python
4000
import anyconfig
4001
4002
conf1 = anyconfig.load("/path/to/foo/conf.d/a.yml")
4003
```
4004
4005
[_Return to the Top_](#python-cheatsheet)
4006
4007
## Debugging
4008
4009
### Raising Exceptions
4010
4011
Exceptions are raised with a raise statement. In code, a raise statement consists of the following:
4012
4013
- The raise keyword
4014
- A call to the Exception() function
4015
- A string with a helpful error message passed to the Exception() function
4016
4017
```python
4018
>>> raise Exception('This is the error message.')
4019
Traceback (most recent call last):
4020
File "<pyshell#191>", line 1, in <module>
4021
raise Exception('This is the error message.')
4022
Exception: This is the error message.
4023
```
4024
4025
Often it’s the code that calls the function, not the function itself, that knows how to handle an exception. So you will commonly see a raise statement inside a function and the try and except statements in the code calling the function.
4026
4027
```python
4028
def box_print(symbol, width, height):
4029
if len(symbol) != 1:
4030
raise Exception('Symbol must be a single character string.')
4031
if width <= 2:
4032
raise Exception('Width must be greater than 2.')
4033
if height <= 2:
4034
raise Exception('Height must be greater than 2.')
4035
print(symbol * width)
4036
for i in range(height - 2):
4037
print(symbol + (' ' * (width - 2)) + symbol)
4038
print(symbol * width)
4039
for sym, w, h in (('*', 4, 4), ('O', 20, 5), ('x', 1, 3), ('ZZ', 3, 3)):
4040
try:
4041
box_print(sym, w, h)